Originally Posted by
Sk1nyNerd
1. "Invoking" seems like the way to do it. I see the Reflection uses a similar approach now and other source codes as well. I just thought it looked awkward given the way we use Includes here and my lack of experience in other languages. What exactly does invoking do? Point to the methods allocated memory?
2. You are correct, I am "marking" the methods with the static modifier
Definition of "Invoking" = "Calling a method/function" (different in other languages).
First let's define some terms (different in other languages!):
Code Code:
"Access Level" - A keyword that defines the level of access a function, class, or variable allows.. WHO is allowed to access it.
- PUBLIC: Allows ANYONE to access a class, function, or variable.. ANYONE can inherit this class, override functions, or variables.
- PRIVATE: Allows NO-ONE to access a class or function.. Classes in the same FILE can inherit this class.
- PROTECTED: Allows only CHILD classes to access a function.. Only classes in the same FILE can inherit this class.
- DEFAULT (un-specified): Only classes in the same PACKAGE can access this class or function, and inherit from it.
"Accessor" - A keyword that defines HOW a class or function is to be accessed.
- STATIC: This accessor means that you do NOT need an instance of the class to access it. Just do so directly. Static functions CANNOT access instance variables or instance functions.
- FINAL: This accessor means that no one can change it for the duration of its life-time (equivalent to "constant"). It also means you cannot OVERRIDE when applied to a function.
- DEFAULT (un-specified): This accessor means that you MUST create an instance of a class (instantiate) to access this function or variable.
Let's take for example:
Java Code:
package com.skinny.nerd.concurrency;//This class can only be accessed from within THIS package! //Why? Because it is has no "Access-Level" so the default "Access-Level" is: "DEFAULT".//If you wanted it to be accessed from any package, you'd declare it as "public class Thread {...}"//Since we never specified an accessor keyword, it becomes "DEFAULT: Package-Private".class Thread { //These are private and cannot be accessed by anyone except for this class itself. Even child classes cannot access it. //These also have an accessor and can be called directly: `Thread._nativesleep(100);` without an instance. private static native void _nativesleep
(int ms
); private static native void _nativeyield
(); //Can be accessed from ANY package because it has a PUBLIC accessor with respect to its class' access level. //Since the class is package-private, this is package-private as well. public static void sleep
(int ms
) { _nativesleep
(ms
); } //Can be accessed from only this package because it has no "access-level". Therefore it takes "Default". Even if this class was "PUBLIC", this function itself would have "DEFAULT" access still. //Now this function has an "accessor" applied to it. It's "static". This means you can just do "Thread.yield()" instead of having to instantiate an instance: "Thread t = new Thread(); t.yield();" <--- CAN'T! Because it's static! `Yield` - Cannot be called on an instance. static void yield
() { _nativeyield
(); } //Can be accessed from any package. However, CHILD classes cannot override this function! Also, since this class is "DEFAULT".. This method is also "DEFAULT" and can only be accessed within this package. public final void run
(Runnable r
) { r.
run(); }}//This class can be accessed from ANYWHERE and by ANYONE.. Why? Because it is "public".//This call is FINAL so NO ONE can inherit it!public final class SynchronousQueue
{ private Lock m
= null; private static SynchronousQueue instance
= new SynchronousQueue
(); //NO ONE can instantiate this class.. except this class itself! private SynchronousQueue
() { this.
m = new Lock
(); } //Public so this function can be called by anyone. //Static so that we can do `SynchronousQueue.getSystemQueue()` since the constructor is private and we CANNOT create instances otherwise.. //Final so that NO ONE (children) can override this function.. public static final SynchronousQueue getSystemQueue
() { return instance
; //return our static instance. } //Protected so that child classes can access this function. //NOT final so that child classes can override this function and return a different value if they want to.. //However, since our class itself is `FINAL`, then NO ONE can even inherit it anyway so no one can override this function.. /Therefore
this function is essentially
private! protected boolean isLockFree
() { return Native.
isLockFree(this); }}
Now let's say you have another class in the same package, it has the following abilities for invoking the above classes' functions:
Java Code:
package com.skinny.nerd.concurrency;class AsyncQueue
{ public AsyncExecutor
(Runnable r
) { Thread.
sleep(100); //Doesn't require a `new Thread()` to call it. Can be accessed from any package because it has a PUBLIC accessor. Thread.
yield(); //Doesn't require a `new Thread()` to call it. Can only be accessed from THIS PACKAGE. Thread t
= new Thread(); //Create an instance of a Thread. t.
run(new Runnable() {...
}); //Call `run` on that instance. `run` requires an instance because it is NOT static. SynchronousQueue queue
= new SynchronousQueue
(); //ERROR!!! Constructor is private! You CANNOT create an instance of `SynchronousQueue`!! SynchronousQueue queue
= SynchronousQueue.
getSystemQueue(); //We now have an instance of `SynchronousQueue`.. queue.
isLockFree(); //CANNOT access `isLockFree` because it is protected! }}
A class from OUTSIDE that package has abilities such as:
Java Code:
package com.skinny.nerd.main;class Main
{ public static void main
(String[] args
) { Thread t
= new Thread(); //ERROR!!!!! Main CANNOT "see" `concurrency.Thread`. Why? Because `class Thread` is package-private! Thread.
sleep(); //Same ERROR! Main CANNOT "see" `concurrency.Thread` still.. In other words, `Thread` is completely invisible! SynchronousQueue queue
= SynchronousQueue.
getSystemQueue(); //Yay! `SynchronousQueue` is visible to us because it is `public`. queue.
isLockFree(); //ERROR: This function is protected and cannot be accessed! }}
An `$include` statement in Simba is equivalent an `import` statement in Java.
A include such as the `Reflection` include (grouped into folders) is equivalent to a `package`/`module` in Java.