Welcome readers,
There are 6 ways by which we can call a method from another class in Java.
1. Calling a method from another class in Java
In this scenario, class B wants to access class A’s method.
To do so, we created a caller method (caller_Method) in class B.
Then, we created an object of class A (a_obj) in this caller method (caller_Method).
Then by using this object we called class A’s method (simple_Method).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
void simple_Method() | |
{ | |
System.out.println("I am a simple method"); | |
} | |
} | |
class B { | |
void caller_Method() | |
{ | |
A a_obj= new A(); | |
a_obj.simple_Method(); | |
} | |
} |
2. Calling a method from another class’s main method in Java
In this scenario, class Main wants to access class A’s method.
To do so, we created an object of class A (a_obj) in the main method of class Main.
Then by using this object we called class A’s method (simple_Method).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
OUTPUT:
I am a simple method
3. Calling a static method from another class’s main method in Java
There are scenarios in which we do not want to create an object of the other class in order to call its method.
In such a case, we make the method static.
Here, we kept the method of class A, static, so that we could call it without creating its object.
And hence in the main method of class Main, we directly called the static method [A.simple_Method()]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
OUTPUT:
I am a simple method
4. Calling a private method from another class in Java
Calling a private method from another class is not possible because private methods cannot be accessed outside the class in which they exist.
5. Calling a public method from another class in Java
Now, this is a very easy example and kind of repetition of what we just did. The only difference is that the method of class A is public and hence we can access it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
OUTPUT:
I am a simple method
6. Calling a protected method from another class in Java
Since we need to access a protected method from another class, we need to extend A.
Why? because a protected method can only be accessed by a subclass.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
OUTPUT:
I am a simple method
In this java tutorial, we learned how to use method from another Java class.
I hope this article helped you.
In case the solution is not clear to you, I suggest you take a live 1:1 lesson on this subject from me. I am an Oracle-certified tutor who would love to teach this stuff to you.
Aniket Malik