Method in Java

Table of Contents

Method in Java

What is a method in Java?

method is a block of code or statements or a set of code meant to perform together with a certain task or operation. Although it can be used in various ways the most important out of it is to increase the reusability of code. This only feature allows us to write a method once and use it many times.

But the method can be executed only when it is been called or invoked.

Who calls the main method?

So, this point might raise a question in your mind that if it is so then who calls the main method. The answer is very simple that it is been called by the Operating System, but in the case of Java, this work is done by JVM only.

Now let us understand the syntax of writing a method.

Syntax of a method

The syntax of declaring a method is in the following order

  • Access Modifiers(public, private, protected or default)
  • Constants if needed then(if you remember that how a constant is called in Java, with the help of keywords such as static and final)
  • Datatype or return type(int,char,float,double ,void etc can be used)
  • Name of the method, it depends on you what you chose but keeps this in mind that you must not use any of the 48 keywords.
  • Parameters with there return types
  • Method body where the entire code related to the particular method will be written.

Now let us look an example,to check the points listed above,

public void Simple(int a,int b)
{
  //Method Body
}

Here, public is the access modifier, void is the return type of the method,sum is the name of the method and int a , int b are the parameters that will be accepted by the method with their datatypes.

Types of Method

There are two types of methods in Java:

  • Predefined Method
    • Predefined methods are the method that is already defined in the Java class libraries, also known as the standard library method or built-in method.
    • Whenever we need any method, we can directly use these methods just by calling them in the program at any point.
    • Sometimes, to use those methods we need to import the entire package or even a chunk of it.
    • print() is the basic example of predefined datatypes that belongs to java.io.PrintStream class.
  • User-defined Method
    • This can be anything whatever type of service do the programmer needs, provided that the programmer can’t use the keywords.

To understand the User-defined methods lets see an example:

class Add  
{  
  //user defined method  
  public int add(int num1, int num2) //num1 and num2 are parameters  
  {  
    int sum;  
    sum=num1+num2;  
    return sum; //returning the sum  
  }
  public static void main(String[] args)   
  { 
    int a,b,c;
    a = 1;  
    b = 2;  
    c = add(a, b);  //method calling  
    System.out.println("The sum of a and b is= " + c);  
  }    
}  

In the above example, add is the user-defined method that has been called from the main method, and the result driven by the method is then returned to the main method(from where it was called).

Previous Object and Class in Java
Next Constructor in Java

Leave a Reply