Tutorials

Java Basics


Java Method


A method in java can be thought of as a group of statements that can be called as per requirement to perform some productive task.

We have just seen main() method which is entry point for a java program on previous page. JVM calls it to start the program execution.

 

Java provides many in-built methods to help us improve efficiency and save time. For example, println()

Similarly, we can write different methods in java for various purposes.

 

We have two types of methods in java:

  1. Simple method (no parameter)
  2. Parameterized method - Here parameter can be passed as call by value or call by reference.

We will see example for both the methods.

 

Syntax 1: Simple method


accessModifier  returnType  methodName(){

//method body

}

Example: Given below is the simple method 'repair( )' in TVMechanic class which can be called by any other class, say class Person, Person1, etc. by creating instance of TVMechanic class. See the code snippet for reference.


package methodExample;

class Person {

      public static void main(String[] args){

            System.out.println("Fault in TV set");
            TVMechanic mechanic = new TVMechanic();

            System.out.println("Call TV mechanic");
            mechanic.repair();

      }

}



class TVMechanic {

      public void repair(){

            System.out.println("Trace the fault");
            System.out.println("Repair the fault");

      }
}

OUTPUT:

Fault in TV set
Call TV mechanic
Trace the fault
Repair the fault

 

Syntax 2: Parametrized method in java


accessModifier  returnType  methodName(dataType param1, ..., dataType paramN){

//method body

}

Example: Given below is an example where we pass two parameters while calling add method of Adder class.


package methodExample;

public class paramExample {

      public static void main(String[] args) {

            int firstNum = 10;
            int secondNum = 20;

            System.out.println("Call parameterized method to add");

            Adder adder = new Adder();
            adder.add(firstNum, secondNum);

      }

}



class Adder{

      public void add(int num1, int num2){

            int result;
            result = num1 + num2;

            System.out.println("Result is : "+ result);

      }

}

OUTPUT:

Call parameterized method to add
Result is : 30

 

Important points on methods in java

  • Every method must be given a proper name. By naming convention, it should be written in camel case, where first letter should be small. For example, multiply(), myMethod(), thisIsMethod()
  • A method in java may or may not return a value. This is defined by the return type. If your return type is 'void' then method will not return anything. For String, int etc the method will return a value with respect to return type declaration at method level.
  • A method in java may or may not contain parameter.
  • Access modifier in java defines the scope or visibility of the method to same or other classes.

Why method is needed?

A method can be considered as an expert who knows its job the best. Whenever you want to perform that particular job then you can simply call the method and leave the rest to it.

 

For example, say you have problems in your television set. So in this case you will call the TV mechanic and rest assured that he will fix the problem. TV mechanic can be thought of as a method who has entire set of code written in him to fix any problem in television. 

Same television mechanic can be called by different person. 

 

This is similar to a method (i.e mechanic) being called by different person (i.e class)

     

Advantages of java method

  • Giving a proper method name increases the readability and understandibility of the purpose of the method. Say multiply(int x, int y) here you will immediately understand that this method will multiply two integer values.
  • It increases re-usability of the code thus also reduce size of the code. 
  • Complex problem can be modularized with the help of method.
  • In case code is written without using method, it will grow very large in size and thus would also become difficult to test, debug and maintain the code.


Please Share this page
Views : 261
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!