How to Call a Method in Java From Another Class

How to Call a Method in Java From Another Class. This application explains how a method can be called from within a single class. You must first create a class called ‘Main’ within which you will call the main() method in this application. 

This main() method also calls the Method1() and Method2() methods (). You may now refer to this as a method definition that makes a call to another set of methods. This program will help you how to call a method in java from the same class. 

public class Main
{
public static void main(String[] args)
{
Method1();
Method2();
}
public static void Method1()
{
System.out.println("Welcome to inlarn");
}
public static void Method2()
{
Method1();
}
}

 

Output:

Welcome to inlarn

Welcome to inlarn

Method in Java

A method is a collection of Java statements that perform an operation on data and may or may not yield a result. A method is a group of statements that execute a certain job and provide the outcome to the caller. A method can carry out a given job without delivering any results. Methods allow us to reuse code without having to retype it.

Method Declaration in Java

A method definition, like a class, has two key parts: the method declaration and the method body. As demonstrated in the picture below, the method declaration describes all of the method’s properties, including access level, return type, name, and parameters. The method body is where all of the action happens. Using this program we can execute how to call a method in java.