Date:

Share:

A Tutorial on Calling Java Methods from Other Classes

Related Articles

Methods and functions are a set of instructions that are executed when someone calls them. A method can have different instructions that are combined to perform a certain task. The code specified in the method is executed when it is called.

We have two types of methods in Java: user-defined and predefined. A user-defined method is a method defined within the class to perform a specific function. In contrast, predefined methods can be called and used anywhere in the program without defining them.

A method can be invoked within the same class or from another Java class. Each method, whether user-defined or predefined, is called using the dot syntax. This article has been adapted to present an in-depth overview of how to call a method in Java from another class with examples.

parts of a method

  1. Mishna: There are four types of modifications in Java: public, private, protected, and default. Static is a non-accessible modifier. Modifiers can change the behavior of a class. A method can be attached to one or more variables.



  2. Return type: Every method has a return type. The return type refers to the information returned to the caller after the job is finished. If nothing is to be returned, it can be set to void



  3. method name: This is required because the method will not be called without a name. A method name starts with an alphabet, not a special character or digit. An underscore is an example of a special character.



  4. List of arguments: If the method requires some information from the caller to perform a task, you can specify one or more arguments. The arguments are provided in a list enclosed in parentheses, where an argument name follows the argument type. Commas separate multiple arguments.



  5. List of exceptions: This is a list showing the exceptions that the method will encounter.



  6. Method body: This is code that defines the tasks a method should perform. It is always surrounded by a curly brace.

Calling a method from another class in Java

Method 1: How to call a default method from another class in Java

In case the method has no change, it is taken as default. To call a default method of another class, you need the object of the class. A default method can be accessed from inside the package and not from outside it. This method is more accessible than the private method but more restrictive compared to protected and public methods. Let’s use the “School” class which has a method named “getName()”. To access this method from the second class, let’s say “Tests”, we use an object of the Student class. The program below is used to illustrate how a default method can be called from another class.

class School{
	String name;
	School(String name){
		this.name = name;
	}
	public String getName() {
		return this.name;
	}
}
public class Testing {
	public static void main(String[] args) {
// creating an object of the class School
		School school = new School("Cheptikit");
// calling the method getName of the class School
		String name = school.getName();
		System.out.println("The school name is : "+name);
	}
}

Productivity

The school name is : Cheptikit

Method 2: How to call a static method in Java from another class

In this scenario, we call a static method of another class. We don’t need to create objects to call method in case of static method because static method can be accessed directly with class name without creating object of that class. A static method directly accesses static data. However, it requires an object to access the instance. The example below illustrates how to call a static method from another class in Java.

class School{
	static String name;
	static String getName() {
		return name;
	}
}
public class Testing {
	public static void main(String[] args) {
		School.name = "Cheptikit";
		String name = School.getName();
		System.out.println("The School name is : "+name);
	}
}

Productivity

The School name is : Cheptikit

Method 3: How to call a protected method from another class in Java

If a method of a class has been declared protected, it can be called in another class only if that class is a subclass of the class that has the protected method. The protected method provides more accessibility compared to the private method. The program below illustrates how to access a protected method from another class.
In this case, we extend the “School” class to the “Testing” class and call the getName() method using the “Testing” class object. Let us clarify this with an example.

class School{	
	protected String name;
	protected String getName() {
		return this.name;
	}
}
public class Testing extends School{	
	public static void main(String[] args) {
// creating an object of Class Testing
	    Testing tt = new Testing();
		tt.name = "Cheptikit";
// calling getName() method of class School
		String name = tt.getName();
		System.out.println("The School name is : "+name);
	}
}

Productivity

The School name is : Cheptikit

Method 4: How to call a public method from another class in Java

A method is called public if it can be called by another class and if it is available for external access. The public method can be accessed from inside or outside the package.
The example below shows how to access the public method from another class.
In this example, let’s call a public method called getName() for another class using the school class:

class School{	
	public String name;
	public String getName() {
		return this.name;
	}
}
public class Testing{
	public static void main(String[] args) {
// creating an object of the Class School
		School sch = new School();
		sch.name = "Cheptikit";
// calling the getName method of the School Class
		String name = sch.getName();
		System.out.println("The School name is : "+name);
	}
}

Productivity

The School name is : Cheptikit

Method 5: How to call a private method from another class in Java

A private method has more accessibility restrictions compared to other methods. A private method can be called from any class using java.lang.Class and java.lang.reflect.Method using getDeclareMethod(), setAccessible() and invoke() methods. The program below illustrates how to call a private method from another class. Private methods cannot be called on any other class because they are restricted to that class.

import java.lang.reflect.Method;
class School {
  // Private method
  private void printData() {
    System.out.println("Cheptikit is the best Elementary School");
  }
}
public class Example {
  public static void main(String[] args) throws Exception {
    School sch = new School();
    // Using getDeclareMethod() method
    Method m = School.class.getDeclaredMethod("printData");
    // Using setAccessible() method
    m.setAccessible(true);
    // Using invoke() method
    m.invoke(sch);
  }
}

Productivity

Cheptikit is the best Elementary School

Summary

A method is accessible from another class based on the access modifier. For example, a method created with a public variable can be called from inside or outside the class. By using inheritance, a protected method can be invoked from another class, while a static method can be invoked from another class by using the class name. Private methods cannot be called for any class because they are restricted to that class. This article looked at various examples in explaining how to call a method from another class in Java. I hope this helps you solve your puzzle.

Source

Popular Articles