Method

The method is a block of code that performs a special task. And the method has the name by which we can call it.

For example

public static void Add() {
    int first = 10;
        int second = 20;

        int sum = first + second;

        System.out.println("The sum is: " + sum);

}

Here the name of the method is add
it is a block of code
the task is to add two integers and print it

Why we use methods

  • We use method to  make code reusable code
  • The benefit of the method is we can use a block of code anywhere in the program by calling it.
  • We don’t have to write the same code repeatedly

Type of method

type of method in java

Built in methods :

This methods is already define in java standard libarary

Simpily we use this method by calling its name

Some built in methods are :

Printlln();  it print any thing on output screen

Sqrt();  it returns square root of number pass in it

See example of built in method:

public class Main {
    public static void main(String[] args) {
    
        // using the sqrt() method
        System.out.print("Square root of 4 is: " + Math.sqrt(4));
    }
}

output

Square root of 4 is: 2.0

User define method :

We can also create a methd by self which perform our desire task

which method is created by the programmer to perform a special task is called user define method

How to create method :

This is the syntax of creating a method

modifier returnType nameOfMethod (Parameter List) {
  
   // method body
}
  • Here modifier defines the access type  like public, private protected
  • Return type defines what method returns after execution
  • Name of the method  we declare as we declare the variable name

Example of user define function:

class Output {
  
    // public: this method can be called from outside the class
    public void myMethod() {
        System.out.println("Printing from inside myMethod().");
    }
}


//another class
class Main {

    public static void main(String[] args) {

        // create object of the Output class
        Output obj = new Output();
        System.out.println("About to encounter a method.");

        // calling myMethod() of Output class
        obj.myMethod();

        System.out.println("Method was executed successfully!");
    }
}

output

About to encounter a method.
Printing from inside myMethod().
Method was executed successfully!

Some merhod accept argument and return a value  

Example:

class SquareMain {
    public static void main(String[] args) {
        int result;

        // call the method and store returned value
        result = square(); 
        System.out.println("Squared value of 10 is: " + result);
    }

    public static int square() {
        // return statement
        return 10 * 10;
    }
}

output

Squared value of 10 is: 100

Example: Method Accepting Arguments and Returning Value

public class Main {
   
    public static void main(String[] args) {
        int result, n;
        
        n = 3;
        result = square(n);
        System.out.println("Square of 3 is: " + result);
        
        n = 4;
        result = square(n); 
        System.out.println("Square of 4 is: " + result);
    }

    // method 
    static int square(int i) {
        return i * i;
    }
}

output

Squared value of 3 is: 9
Squared value of 4 is: 16



SHARE THIS PAGE





Important Link