Java - Static methods

Java

In this tutorial we will learn about static methods of a class in Java programming language.

In the previous tutorial we learned about the static variable of a class. Feel free to check that out.

When we want to have a class method that can be used independently of any object of the class then we create a static method.

Static methods have the following restrictions.

  • Static methods can call only static methods.
  • Static methods can access only static variables.
  • We can't use the this and super keywords in the static methods.

Note! The super keyword is used to call the constructor of the parent class from the child class. We will learn about the super keyword in the inheritance tutorial.

Syntax of static methods

static returnType methodName() {
  // some code ...
};

Where, returnType is some valid return type and methodName is the name of the static method.

Calling the static method

To call the static method we have to use the following syntax.

ClassName.methodName();

Where, ClassName is the name of a class and methodName is the name of the static method of the class.

Example

In the following example we have the HelloWorld class and it has a static integer variable objectCount. The value of this static variable is incremented whenever a new object of the class is created. We also have the getCount() and resetCount() static methods to get and reset the value of the objectCount variable respectively.

class HelloWorld {

  // static variable
  static int objectCount = 0;

  // variable
  private int number;

  // constructor
  HelloWorld(int number) {
    this.number = number;

    // updating the static variable
    HelloWorld.objectCount++;
  }

  // static method
  static void resetCount() {
    objectCount = 0;
  }

  static int getCount() {
    return objectCount;
  }

  // method
  public int getNumber() {
    return this.number;
  }
}

public class Example {
  public static void main(String[] args) {

    System.out.println("Total number of objects of the Hello World class: " + HelloWorld.objectCount);

    // create an object
    System.out.println("Creating object obj.");
    HelloWorld obj = new HelloWorld(10);

    System.out.println("Total number of objects of the Hello World class: " + HelloWorld.objectCount);

    System.out.println("Hello World obj number: " + obj.getNumber());

    // create another object
    System.out.println("Creating object obj2.");
    HelloWorld obj2 = new HelloWorld(20);

    System.out.println("Total number of objects of the Hello World class: " + HelloWorld.objectCount);

    System.out.println("Hello World obj2 number: " + obj2.getNumber());

    System.out.println("Resetting object count of the Hello World class.");
    HelloWorld.resetCount();
    System.out.println("Total number of objects of the Hello World class after reset: " + HelloWorld.getCount());

  }
}

Output:

$ javac Example.java 
$ java Example
Total number of objects of the Hello World class: 0
Creating object obj.
Total number of objects of the Hello World class: 1
Hello World obj number: 10
Creating object obj2.
Total number of objects of the Hello World class: 2
Hello World obj2 number: 20
Resetting object count of the Hello World class.
Total number of objects of the Hello World class after reset: 0

So, in the above code we are creating two objects of the HelloWorld class.

Each time we instantiate an object, we increment the value of the static variable objectCount by 1 using the ++ increment operator.

Finally, we call the resetCount() static method to reset the static variable and getCount() static method to get the value of the static variable.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT