# Thread: [Java] Proper Way to Call Methods from Other Classes

1. ## [Java] Proper Way to Call Methods from Other Classes

Hello,

I'm continuing my learning of Java and now that I have a lot of methods it's getting a little cumbersome to keep copying and pasting them at the end of every new script so I'm looking to make something like we have here called an Include.

What is the convention used in Java for calling "methods" from "other classes" within the same "package" but in a "new class"? I was under the impression that if all the classes are in the same package you do not need to do anything if the methods are "public" or "protected" but that does not seem to be the case as I get an error on every public method I call from another class.

I have found "importing" the class does not do anything, which makes sense because both Mouse and Time are in the same package called Core.
Code:
import Core.Time;
I have found that calling "extend" works but if I wanna call from multiple classes or packages I run into trouble.
Code:
public class Mouse extends Time
Code:
public class Mouse extends Core.Time
I have found that I can use periods to establish a "heiarchy" that allows the class to access another class or package but in my opinion it looks messy.
Code:
Time.waitBetween(50,130);
Code:
Core.Time.waitBetween(50,130);
Thanks

2. @Sk1nyNerd, on disk:
Code:
.|-- core|   -- Time.java-- Main.java

In Main.java:
Java Code:
import core.Time;public class Main {    public static void main(String[] args) {        core.Time inst0 = new core.Time(); // Does not require import statement.        Time inst1 = new Time();    }}

In Time.java:
Java Code:
package core;public class Time {};

Compile and run:
Code:
javac Main.javajava Main

You will be able to use javac by itself for quite some time, but it is at this point you should learn to use Apache ANT or Apache Maven. Look at the default project layouts and see if they suit your taste.

I dislike Maven's automatic dependency handling, but it is the most reasonable thing to use on Windows. Porting Maven builds to Linux distributions tends to be very hard (though running Maven is generally no trouble). Ant with Ivy is like Maven. Ant imposes less restrictions on your build process, but takes more work to configure.
Last edited by R0b0t1; 02-26-2018 at 03:22 AM.

3. Java is object orientated. There will generally always be a class or instance name before the method you're trying to invoke (instanceName. ClassName. etc) unless you're invoking a method in the super(or parent) or same class.

You should also mark methods with the static modifier (public static void waitBetween(...) etc) to access them like Time.waitBetween(...). (It seems like you're doing this anyways). That way you won't have to instantiate every time you need to call a method that is threadsafe / doesn't modify none static memory (new Time().waitBetween(...)).
Last edited by Kasi; 02-26-2018 at 04:02 AM.

4. Originally Posted by R0b0t1
@Sk1nyNerd, on disk:
Code:
.|-- core|   -- Time.java-- Main.java

In Main.java:
Java Code:
import core.Time;public class Main {    public static void main(String[] args) {        core.Time inst0 = new core.Time(); // Does not require import statement.        Time inst1 = new Time();    }}

Compile and run:
Code:
javac Main.javajava Main

You will be able to use javac by itself for quite some time, but it is at this point you should learn to use Apache ANT or Apache Maven. Look at the default project layouts and see if they suit your taste.

I dislike Maven's automatic dependency handling, but it is the most reasonable thing to use on Windows. Porting Maven builds to Linux distributions tends to be very hard (though running Maven is generally no trouble). Ant with Ivy is like Maven. Ant imposes less restrictions on your build process, but takes more work to configure.
1. I'm not sure what you mean by "on disk" ? Looks like hierarchy representation?

2. I saw instantiating as a popular recommendation elsewhere too but it does not make sense to me to rename a method as an object that does the exact same thing. It seems like unnecessary memory use and an unnecessary headache down the road for back tracking names. But I do not know, that's why I am here asking

3. You kind of went over my head with the different compilers. I have yet to get into that area and probably wont for quite some time.

Originally Posted by Kasi
Java is object orientated. There will generally always be a class or instance name before the method you're trying to invoke (instanceName. ClassName. etc) unless you're invoking a method in the super(or parent) or same class.

You should also mark methods with the static modifier (public static void waitBetween(...) etc) to access them like Time.waitBetween(...). (It seems like you're doing this anyways). That way you won't have to instantiate every time you need to call a method that is threadsafe / doesn't modify none static memory (new Time().waitBetween(...)).
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

5. 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.
Last edited by Brandon; 02-27-2018 at 02:11 AM.

6. Originally Posted by Brandon
Definition of "Invoking" = "Calling a method/function" (different in other languages).

First let's define some terms (different in other languages!):

.........
Phenominal, thank you!