
In Java, calling one function (method) from another is straightforward. You can call a method either from within the same class or from another class. Below are several examples with explanations of how this works in different contexts.
1. Calling a Method from the Same Class
If you have two methods in the same class, you can call one method from another by simply using the method name.
public class Example1 {
public static void main(String[] args) {
Example1 obj = new Example1();
obj.methodA(); // Call methodA from main
}
public void methodA() {
System.out.println("Inside method A");
methodB(); // Calling methodB from methodA
}
public void methodB() {
System.out.println("Inside method B");
}
}
Explanation:
methodA()
callsmethodB()
directly by its name because both methods belong to the same class.- In the
main
method, we create an objectobj
to callmethodA()
, which in turn callsmethodB()
.
2. Calling a Method with Parameters
If the method you are calling has parameters, you pass the necessary arguments during the method call.
public class Example2 {
public static void main(String[] args) {
Example2 obj = new Example2();
obj.methodA(10, 20); // Call methodA with arguments
}
public void methodA(int x, int y) {
System.out.println("Sum in method A: " + (x + y));
methodB(x, y); // Passing the same parameters to methodB
}
public void methodB(int a, int b) {
System.out.println("Product in method B: " + (a * b));
}
}
Explanation:
methodA(int x, int y)
callsmethodB(int a, int b)
and passes the same parameters.- The output shows the sum of the two numbers in
methodA
and their product inmethodB
.
3. Calling a Static Method from Another Static Method
Static methods can be called directly from other static methods without creating an object.
public class Example3 {
public static void main(String[] args) {
methodA(); // Calling static methodA from main (which is also static)
}
public static void methodA() {
System.out.println("Inside static method A");
methodB(); // Calling static methodB directly
}
public static void methodB() {
System.out.println("Inside static method B");
}
}
Explanation:
- Since both
methodA()
andmethodB()
are static methods, they can call each other directly without an object.
4. Calling a Method from Another Class
You can call a method from a different class by creating an object of that class or using a static method.
class Helper {
public void methodB() {
System.out.println("Inside method B of Helper class");
}
}
public class Example4 {
public static void main(String[] args) {
Example4 obj = new Example4();
obj.methodA();
}
public void methodA() {
System.out.println("Inside method A of Example4 class");
Helper helper = new Helper(); // Create an object of Helper class
helper.methodB(); // Call methodB from Helper class
}
}
Explanation:
methodA()
belongs toExample4
and callsmethodB()
from a different class (Helper
).- You create an object of the
Helper
class (helper
) and then callmethodB()
using that object.
5. Calling Private Methods from Within the Same Class
Private methods cannot be called from outside their class but can be invoked from other methods within the same class.
public class Example7 {
public static void main(String[] args) {
Example7 obj = new Example7();
obj.methodA(); // Call public methodA
}
public void methodA() {
System.out.println("Inside public method A");
methodB(); // Calling private methodB from public methodA
}
private void methodB() {
System.out.println("Inside private method B");
}
}
Explanation:
methodB()
is private and is only callable from within the same class, in this case, throughmethodA()
.