Methods in Java

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


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

example of creating simple method

class Test
public void myMethod(int a)

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.

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();

  For static methods object is not required to call these methods. we can call these methods with class name, for example.

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

  static methods are associated with a class.

Non-Static / Instance Method

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

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();

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

Example of assigning values for object using the method.

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

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

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

woman avatar