A method in Java is a block/set of code that gets executed when someone calls it. Calling a method within the same class is quite simple and can be done using the syntax “classObject.methodName()”. However, things get interesting and tricky when a user tries to call a method from another class. In such scenarios, you have to consider/follow the method’s accessibility rules carefully.
Don’t know how to call/access a method from another Java class? No Worries! This guide illustrates various aspects of calling a Java method from another class.
How to Call/Access a Java Method From Another Class
Method calling from another class depends on the method’s scope (access modifier). The accessibility level of the method is illustrated in the following table:
Method Level | Accessibility |
---|---|
Default | It is accessible/callable within the same package only; not accessible from other packages. |
Public | It is accessible/callable throughout your application, i.e., in all classes, subclasses, and from other packages as well. |
Protected | It is accessible/callable in a class in which it is defined and in its child/subclasses. Also, it is accessible/callable in different classes of the same package. |
Private | Accessible/callable only in the same class (in which it’s defined). To access it from other classes, you can use the reflection API. |
Static | Accessible/callable in other classes using the class name (to which it belongs). |
Let’s comprehend each of the stated levels via practical examples. But before that, go through the linked guide if you don’t know how to create a method in Java.
Invoke/Call a Default Method From Another Class
The default method is created without any access modifier like public, private, etc. A default method is accessible throughout the package. However, it can’t be accessed from a different package. For better understanding, let’s create a couple of classes within the same package and name them “Developer”, and “JavaDeveloper”, respectively:
package javaBeat;
//First Class
class Developer {
//Default Method
void empSal()
{
System.out.println("Developer's Basic Salary: 50000");
}
}
public class JavaDeveloper {
public static void main(String args[]) {
// Object Creation
Developer dev = new Developer();
//Calling the default method of Developer Class
dev.empSal();
}
}
First, we create a default method “empSal()” in the Developer class. After this, we create an object of the Developer class in the main() method of the JavaDeveloper class. Finally, we call the empSal() using the object of the Developer class:
Invoke/Call a Public Method From Another Class
The public method is the most flexible method which can be accessed/called within the same as well as from different packages. It is created using the “public” keyword.
Example 1: Calling a Public Method From Another Class of the Same Package
Let’s create a public method “showMessage()” in the JavaBeat class and call/access it from another class “JavaDeveloper” of the same package:
package javaBeat;
//First Class
class JavaBeat {
//Public Method
public void showMessage()
{
System.out.println("Welcome to javabeat.net");
}
}
public class JavaDeveloper {
public static void main(String args[]) {
// Object Creation
JavaBeat jb = new JavaBeat();
//Calling the default method of Developer Class
jb.showMessage();
}
}
Now create an instance of the JavaBeat class in the main() method of the JavaDeveloper class. After this, use this object with the dot syntax to call the empSal() method of the JavaBeat class:
Example 2: Calling a Public Method From Another Class of Different Packages
In this example, we create two classes in two different packages and access a public method of one class into another. For this, we create a public method “showMessage()” in the Example1 class of the “javabeat” package:
package javabeat;
public class Example1 {
//public method
public void showMessage() {
System.out.println("This is Example1 Class of javabeat package");
}
}
After this, we create an “Example2” class in a different package “javageek”. We import the “Example1” class of the “javabeat” package into the “javageek” package using the “import” statement:
package javageek;
//importing Example1 class from javabeat package
import javabeat.Example1;
public class Example2 {
public static void main(String args[]) {
// object creation
Example1 ex = new Example1();
// calling public method of Example1 class of javabeat package
ex.showMessage();
}
}
Finally, we create the object of the imported class and call the desired method using that object:
Note: While calling a method from different packages, make sure that both packages are in the same Java project, otherwise, you may encounter an error.
Invoke/Call a Protected Method From Another Class
You can call/access a protected method within the same class or from another class of the same package. It is also accessible in its child/subclasses. It is created using the “protected” keyword.
Example: Calling a Protected Method From Another Class
First, we create a “Developer” class with a protected method empSal() and then a JavaDeveloper class with the main() method:
package javaBeat;
public class Developer {
// Protected Method
protected void show() {
System.out.println("This is the show() Method of Developer Class");
}
}
class JavaDeveloper extends Developer{
public static void main(String args[]) {
// Object Creation
Developer dev = new Developer();
// Calling the Protected method of Developer Class
dev.show();
}
}
We create an object of the Developer class in the JavaDeveloper class and call the empSal() method using that object:
Note: Similarly, you can inherit a class using the extend keyword and call a protected method from a subclass.
Invoke/Call a Private Method From Another Class
A private method is hidden from other classes and is not directly accessible to any other class or subclass. However, you can access it by modifying the runtime behavior of the class (to which it belongs) using Reflection API. Let’s understand this concept practically using the code:
package javaBeat;
import java.lang.reflect.Method;
class CreatePrivateMethod {
// Private method
private void showMessage() {
System.out.println("Welcome to JavaBeat.net");
}
}
public class CallPrivateMethod {
public static void main(String[] args) throws Exception {
CreatePrivateMethod obj = new CreatePrivateMethod();
//getting the showMessage() of CreatePrivateMethod class
Method method = CreatePrivateMethod.class.getDeclaredMethod("showMessage");
//setting the accessible object for the flag
method.setAccessible(true);
//invoking the method reflected by the method object
method.invoke(obj);
}
}
In this coding example:
- First, we create a class “CreatePrivateMethod” that contains a private method “showMessage()”.
- Next, we create a class “CallPrivateMethod” that keeps a main() method. And within the main() method, we create an object of the CreatePrivateMethod class.
- After this, we use the getDeclaredMethod() to get/access the showMessage() method of the CreatePrivateMethod class (regardless of its visibility).
- Finally, we call the invoke() method on the obj using reflection:
Invoke/Access a Static Method From Another Class
A static method is accessible to other classes without creating the class instance. This means you can access or call a static method of one class to another class directly by using the class name. The accessibility scope of the static method depends on the access modifier used with it, such as public, private, etc.
Let’s create a class “Example1” with a static method “show()”:
package javageek;
class Example1 {
//public static method
public static void show() {
System.out.println("This is public static method of class Example1");
}
}
public class Example2 {
public static void main(String args[]) {
//calling static method of class Example1
Example1.show();
}
}
The show() method of the Example1 class is called/accessed from the Example2 class. As a result, we get the following output:
Similarly, you can create a protected, private, or default static method and call it from another class accordingly.
Final Thoughts
In Java, a method can be static or non-static in nature. The method calling depends on the method’s nature and access modifier. To call/access a non-static method from another class, first, you need to create the class instance (in the class from where you want to invoke it). Up next, you can call the desired method from another class depending upon its access modifier.
Similarly, you can call/access a static method from other classes without creating the class instance. This means a static method of one class can be accessed or called to another class directly by using the class name. The accessibility scope of the static method depends on the access modifier used with it, such as public, private, etc. In this Java guide, all the stated concepts are illustrated with practical examples.