Methods in Java-how to call a method in java

Introducing Method

In java programming language classes usually consist of two things instance variable and methods.

What is a method in java                                                                 

A method is the block of statements that performs a special task.

Using methods we can avoid rewriting the same code over and over in a sane program.

The method increases the efficiency and reduces the complexity of the program. 

Method Definition in Java

following is the Java method syntax:

 accessModifier returnType   methodName(){
     statements; 
}

Write a program to demonstrate the method in the class

 
class RectangleArea {

    int length;
    int width;

    void area() { // method area() definition	
        length = 10;	 // within class variables access without object
        width = 20;
        int a = length * width;
        System.out.println("Area of the rectangle =" + a);
    }
}

public class TestRectangle {

    public static void main(String arg[]) {
        RectangleArea obj1 = new RectangleArea();
        obj1.area();// call area() method	
    }
}

Result

 
Area of the rectangle =200

Creating a method in java

to create a method we have to give a method heading and method body.

As you can already see here

void area() { // method area() definition	
        length = 10;	 // within class variables access without object
        width = 20;
        int a = length * width;
        System.out.println("Area of the rectangle =" + a);
    }

How to call a method in java

In the above program, you can see how the method area is called in main().

Object.methodName(parameter list);

is used to call methods from another/same class.

A static method can be called with a Class name or with the object.

ClassName.methodName(parameter list);

Parameterized(overloaded) Method

Write a program to demonstrate the parameterized method in the class.

 
class RectangleArea {

    int length;
    int width;

    void area(int i, int j) { // method area() definition	
        length = i;	     // within class variables access without object
        width = j;
        int a = length * width;
        System.out.println("Area of the rectangle =" + a);
    }
}

public class TestRectangle1 {

    public static void main(String arg[]) {
        RectangleArea obj1 = new RectangleArea();
        obj1.area(10, 20);// call area() method and pass value of argyments
    }
}

How to call a method in java
Fig: How to call a method in java

Output

 
Area of the rectangle =200

Program to call methods in java Method call with and without agrument and with and without return type

public class MethodsDemo1 {
    public static void main(String[] args) {
        System.out.println("Inside Main method");
        MethodsDemo1 m = new MethodsDemo1();
        m.add();
        int x = 5, y = 7;
        m.sub(x, y); //10,5 are actual parameter
        int mulVal = m.mul();
        System.out.println("Multiplication is " + mulVal);
        int divVal = m.div(10, 5); //10,5 are actual parameter
        System.out.println("Division is  " + divVal);

    }

    //method without argument and without return type
    public void add() {
        int a = 5, b = 5;
        int result = a + b;
        System.out.println("Sum is  " + result);
    }
    //method with argument and without return type
    public void sub(int a, int b) { //a and b are formal arguments
        int result = a - b;
        System.out.println("Subtraction is " + result);
    }
    //method without argument and with return type
    public int mul() {
        int a = 5, b = 2;
        int result = a * b;
        return result;
    }

    //method with argument and with return type
    public int div(int a, int b) { //a and b are formal arguments
        int c = a / b;
        return c;
    }
}

Output

 
D:\Test Java>javac MethodsDemo1.java

D:\Test Java>java MethodsDemo1
Inside Main method
Sum is  10
Subtraction is -2
Multiplication is 10
Division is  2
 

Java Program to pass a object to a method

Student.java

public class Student {
    private long rollNo;
    private String name;
    private String branch;

    public void setRollNo(long rollNo1) {
        rollNo = rollNo1;
    }
    public long getRollNo() {
        return rollNo;
    }
    public void setName(String name1) {
        name = name1;
    }
    public String getName() {
        return name;
    }
    public void setBranch(String branch1) {
        branch = branch1;
    }
    public String getBranch() {
        return branch;
    }
}

StudentDemo.java contains main method and updateStudent method

public class StudentDemo {
    public static void main(String[] args) {
        Student student = new Student();
        student.setRollNo(1);
        student.setName("Ram");
        student.setBranch("CSE");
        System.out.println("Student Details");
        System.out.println("Roll No " + student.getRollNo() + " Name " + student.getName() + " branch " + student.getBranch());
        StudentDemo s = new StudentDemo();
        s.updateStudent(student);
        System.out.println("Roll No " + student.getRollNo() + " Name " + student.getName() + " branch " + student.getBranch());
    }
    public void updateStudent(Student x) {
        x.setRollNo(2);
        x.setBranch("CSE AI ML");
        System.out.println("Roll No " + x.getRollNo() + " Name " + x.getName() + " branch " + x.getBranch());
    }
}

Output

D:\Test Java>javac StudentDemo.java 
D:\Test Java>java StudentDemo
Student Details
Roll No 1 Name Ram branch CSE
Roll No 2 Name Ram branch CSE AI ML
Roll No 2 Name Ram branch CSE AI ML
 

Passing array as argument in java method

public class ArrayArgument{
  public static void main(String[] s){
    int[] numbers={4,5,1,6,2};
	ArrayArgument ar=new ArrayArgument();
	ar.multiply(numbers);
	System.out.println("\nnumbers in main\n");
	for(int a:numbers){
		System.out.print(a+" ");
		}
	}
 	public void multiply(int[] arr){
		for(int i=0;i



Output

 2
 4
 

How to call a void method in java

A void method is a method without a return type. Here public void hello() is a void method it is not returning any value.

To call a void method we follow these steps

  1. Create object of class
  2. call the method with object name
public class MethodCall1 {
    public static void main(String[] args) {
        MethodCall1 m1=new MethodCall1();
        m1.hello();
    }
    public void hello(){
        System.out.println("Hello User");
    }
}
 2
 Hello User
 

How to call a static method in java

to call the static method we can use the following method

  1. Using object as above program does
  2. Using ClassName.methodName as below program
public class MethodCall1 {
    public static void main(String[] args) {
        MethodCall1.hello1();
    }
    public static void hello1(){
        System.out.println("Hello User");
    }
}
 2
 Hello User
 

Instance method java

A method associated with the object is known as an instance method.

In the above example hello() is declared as public void hello() this method is an instance method of the class. This is called using the object of the class.

Method vs function java

Method and functions both are pieces of code that is called by name[ref]. For more details see here

How to call main method in java

In java main is a starting method and its syntax is public static void main(String [] args).

public means it can be accessed from any were.

static means this method can be accessed with class. It can be invoked without using the object of the class.

void - this method doesn't return any value

main - function name.

The main method is automatically get called by JVM where we run our program.

how to call a method in java from another class

public class Calculate {

    private double a;
    private double b;
    private double c;

    public void add(double x, double y) {
        a = x;
        b = y;
        System.out.println("Addition is " + (a + b));
    }
}

public class SumMain {

    public static void main(String[] args) {
        Calculate s = new Calculate();
        s.add(5, 6);
    }
}

Addition is 11.0

Read More

Method Overloading in Java