Important Java Concept

How to use method from another Java class?

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).
 
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).
 
class A {
void simple_Method() {
System.out.println("I am a simple method");
}
}
public class Main {
public static void main(String[] args) {
A a_obj = new A();
a_obj.simple_Method();
}
}
 
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()]
 
class A {
static void simple_Method() {
System.out.println("I am a simple method");
}
}
public class Main {
public static void main(String[] args) {
A.simple_Method();
}
}
 
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.
 
 
class A {
public void simple_Method() {
System.out.println("I am a simple method");
}
}
public class Main {
public static void main(String[] args) {
A a_obj = new A();
a_obj.simple_Method();
}
}
 
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.

 

class A {
protected void simple_Method() {
System.out.println("I am a simple method");
}
}
public class Main extends A{
public static void main(String[] args) {
A a_obj = new A();
a_obj.simple_Method();
}
}
 
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

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?