. Advertisement .
..3..
. Advertisement .
..4..
Calling a method from a different class in Java could be easier and safer when you choose the correct access for calling. In this article, we will show you four different ways to call a method in another class in Java.
Four Ways To Call A Method In Another Class In Java
Calling A Method From Another Class
An instance must be created when we start to call a method from a different class. In this case, we start to access the method getName() with Student (class).Next, use the created object (Student) to call the second class SimpleTesting. Explore the below for more understanding.
>>> class Student{
String name;
Student(String name){
this.name = name;
}
public String getName() {
return this.name;
}
}
public class SimpleTesting {
public static void main(String[] args) {
Student student = new Student("Alex");
String name = student.getName();
System.out.println("Student name is : "+name);
}
}
The output is:
Student name is : Alex
Public Method
As with calling a method from another class, you have to generate an object of that class. Moreover, it is accessed within the package and outside the package as well.
The illustration below will show you how to call a public method from a different class. In this case, we use the Student to call the public method getName() in another class.
class Student{
public String name;
public String getName() {
return this.name;
}
}
public class SimpleTesting{
public static void main(String[] args) {
Student st = new Student();
st.name = "Alex";
String name = st.getName();
System.out.println("Student name is : "+name);
}
}
The output is:
Student name is : Alex
Static Method
Opposing the above method, it seems easier than the public method because we don’t need to set an object for that class when calling a method. Instead, use the class name getName() directly for accessing.
The program below will help you to call a static method from another class:
>>> class Student{
static String name;
static String getName() {
return name;
}
}
public class SimpleTesting {
public static void main(String[] args) {
Student.name = "Alex";
String name = Student.getName();
System.out.println("Student name is : "+name);
}
}
The output is:
Student name is : Alex
Protected Method
The main difference is that the protected method is only applied inside a subclass. That is the reason why this way is more accessible than private access. Here, the Student class is extended into a SimpleTesting class. This allows us to access the getName() method from the object of the SimpleTesting class.
>>> class Student{
protected String name;
protected String getName() {
return this.name;
}
}
public class SimpleTesting extends Student{
public static void main(String[] args) {
SimpleTesting st = new SimpleTesting();
st.name = "Alex";
String name = st.getName();
System.out.println("Student name is : "+name);
}
}
The output is:
Student name is : Alex
Conclusion
You can apply different ways to call a method in another class in Java. Most of them are called into both the same and another class. More importantly, you understand the underlying mechanism and select the proper method for every scenario.
Leave a comment