Method call between classes
Since your class contains its own methods, if you want to call a method from another class, you must use an instance of this class.
For instance:
class A{ public void methodA(){ } } class B{ public void methodB(){ } }
If I want to call methodA() from class B , I have to use this:
public void methodB(){ A a = new A(); a.methodA();
Argument passed between classes
This time methodA() will need an argument, and B as a field that can be used as an argument.
class A{ public void methodA(int argument){ } } class B{ private int fieldB = 42; public void methodB(){ } }
To call methodA() from B , you pass the argument from the class to another.
public void methodB(){ A a= new A(); a.methodA(fieldB);
Results returned between classes
And now methodA() returns the result, this is the code.
class A{ public int methodA(){ return 42; } } class B{ private int fieldB; public void methodB(){ } }
To use / handle the return value of methodA() from class B , you will need to do this:
public void methodB(){ A a= new A(); fieldB = a.methodA();
source share