Methods in Java

A method represents a group of statements that performs a task, here task represents a calculation or processing of data or generating a report, etc.

A method has two parts

  • Method header or Method prototype
  • Method body

Understanding Methods

The below program contains a method without parameters and without a return type

class Sample {
   // instance variable
   private double num1, num2;

   // parameterized constructor
   Sample(double x, double y) {
       num1 = x;
       num2 = y;
   }

   // method to calculate sum of num1 and num2, this method does not accept any values and doesnot return result.
   void sum() {
       double result = num1 + num2;
       System.out.println("Sum =" + result);
   }

    class Method {
       public static void main(String args[]) {
           // create the object and pass values
           Sample s = new Sample(10, 20);
           //call the method and find sum of num1, num2
           s.sum();
       }
   }
}

Here the output will be -

sum = 30

The below program contains a method without parameters but with a return type

class Sample {
   // instance variable
   private double num1, num2;

   // parameterized constructor
   Sample(double x, double y) {
       num1 = x;
       num2 = y;
   }

   // method to calculate sum of num1 and num2, this method does not accept any values but returns the result.
   double sum() {
       return num1 + num2; // return result

   }

   class Method {
       public static void main(String args[]) {
           // create the object and pass values
           Sample s = new Sample(10, 20);
           //call the method and find sum of num1, num2
           double x = s.sum();
           System.out.println("Sum =" + x);
       }
   }
}

Here the output will be -

sum = 30

The below program contains method with two parameters and a return type

 class Sample {

    // this method calculates the sum of num1 and num2, this method accepts double values and also returns the double type result.
    double sum(double num1, double num2) {
        double result = num1 + num2;
        return result; // return result

    }

    class Method {
        public static void main(String args[]) {
            // create the object and pass values
            Sample s = new Sample();
            //call the method and find sum of num1, num2
            double x = s.sum(10, 20);
            System.out.println("Sum =" + x);
        }
    }
}

Here the output will be -

sum = 30

Static Methods

A static method is a method that does not act upon instance variables of a class. A static method is declared by using the keyword ‘static’. Static methods are called using Classname.methodname(). The reason why static method can not act on instance variable is that the JVM first executes the static method and then only it creates the objects. Since the objects are not available at the time of calling the static methods, the instance variables are also not available.

Static Block

A static block is a block of statements declared as ‘static’. JVM executes a static block on a highest priority basis. This means JVM first goes to a static block even before going to the main() method. If a static block is present in a program, JVM will first execute it and then searches for main method and if main method is not present it will throw an error.

’this’ Keyword

’this’ keyword refers to the object of the class where it is used. In other words, ’this’ refers to the object of the present class. Generally, we write instance variables, constructors and methods in a class. All these members are referenced by ’this’. When an object is created to a class, a default reference is also created internally to the object. This default reference is nothing but ’this’. So, ’this’ can refer to all the things of the present object.

Instance Methods

Instance methods are the methods which act upon the instance variables. To call these methods, object is needed, since the instance variables are contained in the object. There are two types of instance methods

  • Accessor methods (getters)
  • Mutator methods (setters)

Accessor methods are methods that simply access or read the instance variables. To store the data into the instance variables, we can use setName(String name) and setAge(int age) methods. These are mutator methods. To read and return the instance variables, we can write methods like getName() and getAge() methods. These are called accessor methods.

Subscribe For More Content