How to call a method from another class in Java

The method of the class can be default, public, static, protected, and private. A method can be called from another class using the object.

Calling a default method from another Class

If the method does not have any modifier, it is taken as default. When the method is a default method, it can be accessed only within the package but it cannot be accessed outside the package.

The default method provides more accessibility than private but is more restrictive than protected and public.

class Bird {
    public void printName() {
    System.out.println("Smallest bird is: Hummingbird");
  }
}
public class Animal {
  public static void main(String[] args) {
    // creating object of class Bird
    Bird obj = new Bird();
    // calling method printName of class Bird
    obj.printName();
   }
}

The output is

Smallest bird is : Hummingbird

Calling a static method from another Class

The static method can be accessed directly by class name without creating an object of that class. A static method can access static data directly but requires an object to access instance.

The example program below illustrates how to call a static method from another class.

class Star {
  static void printInfo() {
    System.out.println("Our galaxy name is : Milky way galaxy");
  }
}
public class Galaxy {
  public static void main(String[] args) {
    // calling a static method by Class name
    Star.printInfo();
  }
}

The output is

Our galaxy name is : Milky way galaxy

Calling a public method from another Class

A public method of a class can be accessed by creating an object of that class. The public method can be accessed by another class.

The public method can be accessed within the package and outside the package also.

The example program below illustrates how to access the public method from another class.

class Student {
    public void Name() {
    System.out.println("Student name is : Joe");
  }
}
public class School {
  public static void main(String[] args) {
    // creating object of Class Student
    Student st = new Student();
    // calling Name method of Student class
    st.Name();
  }
}

The output is

Student name is : Joe

Calling a protected method from another Class

A protected method can be called in another Class only if that Class is a sub-class of the Class which contains the protected method.

The method can be accessed by creating an object for the subclass. The protected method provides more accessibility than private method.

The example program below illustrates how to access protected method from another Class.

class Staff {
  protected void getName() {
    System.out.println("Teacher name is : Alice");
  }
}
public class Teacher extends Staff {
  public static void main(String[] args) {
    // creating an object of Class Teacher
    Teacher t1 = new Teacher();
    // calling getName() method of Class Staff
    t1.getName();
  }
}

The output is

Teacher name is : Alice

Calling a private method from another Class

A private method can be called from another Class by using reflection API. A private method has more accessibility restrictions than other methods.

A private method can be accessed from another class using getDeclaredMethod(), setAccessible() and invoke() methods of the java.lang.class and java.lang.reflect.Method.

The example program below illustrates how to access a private method from another Class.

import java.lang.reflect.Method;
class Demo {
  // Private method
  private void printData() {
    System.out.println("Private Method called from another Class");
  }
}
public class Example {
  public static void main(String[] args) throws Exception {
    Demo d = new Demo();
    // Using getDeclareMethod() method
    Method m = Demo.class.getDeclaredMethod("printData");
    // Using setAccessible() method
    m.setAccessible(true);
    // Using invoke() method
    m.invoke(d);
  }
}

The output is

Private Method called from another Class
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions