How to call a Method in Java


A method in Java refers to a group of rows that performs a single action or action. It is common because it allows code to be reused, which means you can write it once and use it many times. It also allows for easy customization.
Upon calling the given method, it will perform the specified job as soon as the compiler can read the name.

We’ll show you how to run a Java method in this tutorial. In Java, a method is a series of common task completion statements. On the other hand, parameters are data transmitted to the method. Functions is the second name used for methods.

Each procedure or method is given a unique name. When such a name appears in the software, the program execution is diverted to the body of the method. When the method is complete, execution returns to the program code snippet from where it was run, and the program continues to the next line of code. When using the System.out.println () method, for example, the system runs in the background many statements that are already stored in the directory to display a message in the console.

Reading a method in Java

public static void main(String[] args)
System.out.println("execute statements");

public String method1()
  return "method One."

public String method2()
  return "method Two."

Execution process

What are the benefits of using the methods? First, to reuse the code, create it once and use it several times. A modular approach allows different programmers to work on different concepts that can be later incorporated into the construction of the entire project. Implementing the methods will be our first step towards modular programming.

You will now learn how to write your methods with or without repetition values, how to call the method with or without arguments, and how to use abstract abstraction methods in program design.

Creating a method

A declared method within a class. It is defined by the name of the method, preceded by parentheses (). A method definition includes both the title and body of the method.


public class ClassDefinition 
  public static void newMethod() 
    // code to be executed

Explanation of the code:

  • The public static: modifier specifies the access type of the method.
  • newMethod (): The name of the method
  • The method is static because it is a member of the ClassDefinition class and not an object in the ClassDefinition class.
  • This method has no return value; That is why it is called void.

Note that the word public before a method name indicates that the method can be called from anywhere, including other classes and even different packages or files, as long as the class is imported. There are three additional terms used in a public place. They are private and protected. If a method is protected, it can only be called by this class and its sub-classes (classes built on it). If a private method, it can be called only from within the class.

Last but not least, there is a keyword that is not even a word. If you did not have anything in a public, protected or private place, this was the case. This is known as the default setting or private package. This signifies that departments can call the method only from the same package.

How to run a method in Java?

In Java, type the method name followed by two parentheses () and a semicolon to start the method. Reading methods is a simple procedure. The order of events is the method called, taking over from the calling program at the point where the program operates the technique.

When newMethod () is used in the following example, it prints the given text (operation):

public class ClassDefinition 
  static void newMethod() 
    System.out.println("Introduction to using methods in Java!");

  public static void main(String[] args) 

Parameters in Java methods

Information can be provided as a parameter to the methods. Within the method parameters are used as variables. Parameters are provided in parentheses after the method name. The choice of the number of parameters to use is up to you; Use the comma to separate them.

The findProduct method in the following example takes two integers as arguments. When we call a method, we feed it in two numbers that are multiplied by each other within the method:

public class PerformComputations 
  static int findProduct(int num1, int num2) 
    return num1 * num2;

  public static void main(String[] args) 
    int result = findProduct(8, 9);
    System.out.println("The product is: " + result);


When working with multiple parameters, the method call must have the same number of arguments as the parameters and pass the arguments in the same sequence.

public class Employee 
String firstName;
String lastName;
int age;

public Employee()

public Employee(String firstName, String lastName)
    this.firstName = firstName;
    this.lastName = lastName;

public String getFirstName() 
    return firstName;

public void setFirstName(String firstName) 
    this.firstName = firstName;

public String getLastName () 
    return lastName;

public void setLastName (String lastName) 
    this.lastName = lastName;

public int getAge() 
    return age;

public void setAge(int age) 
    this.age = age;

We will now create an instance of the Employee class and use its constructor to initialize its fields, and then use its method to update the field value:

public class Main 

public static void main(String[] args) 
    Employee emp = new Employee("Ann", "Thompson");
    System.out.println("Employee's name is : " + emp.getFirstName());

    System.out.println("Employee's name is: " + emp.getFirstName());

Using arguments in the command line

When you run a program, you may want to enter certain information into it. This is done by running main with command line arguments ().

When a program is executed, a command-line argument is information that appears after the program name in the command line. It’s simple to retrieve command line parameters from a Java program. They are stored in a string array provided to main as strings (). The following is an example that shows all the command line arguments that were run.

public class ArgumentsOnCommandLine 
  public static void main(String args[]) 
      for(int j = 0; j<args.length; j++) 
      System.out.println("args[" + j + "]: " + args[j]);

Bypass methods

Override is a concept that allows a sub-class or descendant class to implement a method that is already proposed by one of its super-classes or parent classes. When a sub-class method has the same name, parameters or signature, and the type of repeat or subtype as a method in its super class, it is said that the sub-class method overrides the super-class method.

// parent class
public class Employee 
//Overridden method
public void earn()

    System.out.println("Every employee is entitled to earn");
// child class

public class HourlyEmployee extends Employee

//Overriding method
public void earn()
    System.out.println("Earns per the number of hours worked");

public static void main( String args[]) 
    HourlyEmployee emp = new HourlyEmployee();
    emp.earn ();

If a method is run with a parent class object, the parent class version is used, but if the method is run with a sub-class object, the daughter class version is used. In other words, which version of a rejected method receives a preference is determined by the type of object to which it refers and not the type of reference variable.

Bypass Java method rules

  • The name of the method must be the same as the method of the parent class.
  • The parameter in the method must be the same as the parent class.
  • There must be a connection between IS and A. (inheritance)

Overload of methods

Method overload occurs when a class contains two or more methods with the same name but different parameters. It’s not the same as overtaking. Bypassing a given method means having the same name, number of parameters, type, etc. Consider the following example, which contains one method for finding the product of numbers of different types.

Public Class PerformComputations

static int findProduct(int a, int b) 
    return a * b;

static double findProduct(double a, double b) 
    return a * b;

public static void main(String[] args) 
    int int_product = findProduct(8, 13);
    double double_product = findProduct(31.5, 12.85);

    System.out.println("int product: " + int_product);
    System.out.println("double product : " + double_product);

If the number and type of parameters are different, many methods with the same name can exist. However, the parameters can still be varied.


A method is a code snippet that only works when executed. It has parameters that relate to data that can be transferred to the method. Methods, often referred to as functions, perform specific tasks. What are the benefits of using the methods? To reuse the code, create it once and use it multiple times.

The method must be created within a class. There is a statement of the method. It is defined by the name of the method, preceded by parentheses (). However, Java has a number of predefined ways, such as system.out.println (), you can write your own to handle specific tasks, as can be seen in our examples.



Please enter your comment!
Please enter your name here