Block of code written for doing a specific task is known as Method in java . Methods are same as functions in C language.

  • Member Methods (behaviors)

    • The methods are like a functions in C-language but in java these functions are called Methods.

    • Inside the class , it is possible to declare any number of methods based on the programmer requirement.

    • Methods are used to provide the business logic of the project.

    • As a software developer while writing method we have to fallow the coding standards like the method name starts with lower case letter if the method name contains more than two words then every inner word starts with uppercase letter.
      for example: printDetail() .
      This naming convention is known as Camel case .

  • Advantage of Method

    • Reusability of the code.

    • By using methods we can optimize our code.

  • Syntax of declaring method


    Syntax:

    
    [modifier-list]  [return-type] [method-name](parameter-list) 
    {
    //body of method...
    }


    example of creating simple method

    
    class Test
    {
    public void myMethod(int a)
    {
    Sytem.out.println("Hello");
    }
    }

  • Method Signature

    The name of the method and parameter list is called Method Signature . Return type and Modifiers list is a not part of a method signature .
    for example

    m1(int a,int b)--->Method Signature
    

  • Type of Methods in java

    Basically, there are two type methods :

    • Static Methods

    • Non-Static Methods

  • Static Methods

    Method defined with the static keyword is called Static Method.

    • For static methods object is not required to call these methods. we can call these methods with class name, for example.
      ClassName.methodName();

    • We can call static methods on object also. but this is not recommended.

    • static methods are associated with a class.

    Example)
    drag_handle
     
    class Test {
        
    //creating static method
        public static void testing() {
            System.out.println("testing ");
        }
    
        public static void main(String[] args) {
            Test.testing();//first way of calling static method
    
            //if we are in the same class then we can call static method directly.
            testing();//second way of calling static method
    
            //we can  call static methods on object also.
            new Test().testing();
        }
    }
    
    Output:
    testing 
    testing 
    testing 
  • Non-Static / Instance Method

    A normal method without static keyword is called Non-static/Instance Method

    • To call non-static methods from the static context object of the class is required.

    Example)
    drag_handle
     class Example {
    //creating non-static method
    
        public void testing() {
            System.out.println("testing ");
        }
    
        public static void main(String[] args) {
            //Object is required to call non-static methods in java
            Example t = new Example();
            t.testing();
        }
    }
    
    Output:
    testing

Example of assigning values for object using the method.
drag_handle

class Person {

    int id;
    String name;

    public void setValue(int i, String n) {

        id = i;
        name = n;
    }

    public void print() {
        System.out.println("Id :" + id);
        System.out.println("Name : " + name);
    }

    public static void main(String[] args) {
        Person p1 = new Person();
        p1.setValue(34, "Ram");//setting values for object using
        p1.print();

        Person p2 = new Person();
        p2.setValue(22, "Shyam");//setting values for object using
        p2.print();
    }

}

Output:

Id :34
Name : Ram
Id :22
Name : Shyam

What next?

The next topic is Constructor in Java

Share this page