Methods in Java Programming

Methods in Java Programming

Methods in Java Programming provide a way to perform specific tasks. A method is a block of code or statements that execute only when called from somewhere in the program. It provides reusability of code and thus saves time and resources.

 

Method Declaration in Java Programming

Method declaration in Java consists of attributes that give information regarding the method’s visibility, return type, name, and parameter(s). The name and parameter(s) of the method are also known as a method signature.

Syntax of method declaration is given below:

public void func(int);

Parts of Method Declaration are as follows:

Method Signature:

Every method has a method signature which consists of a name and a list of parameters.

Access Specifier:

It specifies the access type or visibility of the method. It is also known as an access modifier. In Java, there are 4 types of access specifiers:

      • Public: If the visibility of a method is public, then it is accessible by every java file in an application.
      • Private: If the visibility of a method is private, then it is accessible only inside its class. 
      • Protected: If the visibility of a method is protected, then it is accessible inside its class and by all the classes as long as they inherit the class this method is a part of.
      • Default: If the visibility of a method is public, then it is accessible by every java file that is a part of the package the method’s class is in.

Return Type:

Return type specifies the data type the method returns. It basically specifies the data type of the value the method returns. The return type could be primitive or a reference type. If it is void, then it means the method returns nothing.

Method Name:

It is the name of the method that will perform a specific task. Names of methods should be unique inside a class. It should represent the task at hand and not some random name that explains nothing about its functionality.

Parameter List: 

It is a list that specifies the number of parameters along with the types that will be needed by a method to perform a certain task.

Method Definition in Java Programming

Method definition involves defining method body which describes the instructions that it will perform along with its access specifier, return type, name and parameter list. Syntax of method definition is given below:

public void func(int x){
//method-body
}

 

 

Calling Methods in Java Programming

After method declaration and definition is done, it is called from a place where it is visible which is specified by its access specifier as explained above. Calling or invoking a method involves two things; name of the method and a list of arguments that are passed to it and received by the parameters specified in the method definition. Syntax of method calling is given below:

func(4);

 

 

Methods in Java Example

//Java program with method
public class MethodAdd {
    //performs addition
    static int add(int a, int b){
        return (a+b);
    }
    public static void main(String[] args) {
        //method calling inside System.out.println()
        System.out.println("Addition of 5 and 6 is: "+add(5,6));
    }
}

Output:

Addition of 5 and 6 is: 11

 

 

Static Method in Java

In Java, a static method is created by prefixing the method definition with a static keyword. It belongs to the class rather than an object. Main advantage of a static method is that it can be called without creating an instance of the class. It can access static data members and is invoked by using the class name.

Example of Static Method in Java:

//Java program with method public 
class MethodAdd { 
//performs addition 
static int add(int a, int b)
{ 
return (a+b); 
} 
public static void main(String[] args)
 { //method calling inside System.out.println() 
System.out.println("Addition of 5 and 6 is: "+add(5,6)); 
}
}

Output:

Addition of 5 and 6 is: 11

 

Also, Read our JAVA Course.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *