Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>Static Methods in Java

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



Use of Static Methods in Java with Example Explained

A method declared with static modifier is called static method. The static methods are class level members therefore we can call the static methods using class name.

Syntax:


static returntype methodName()
{
// method body
}


Example:


static int getCounter()
{
return counter;
}

When should I use the static method in Java?

  • If you are writing utility classes and they are not supposed to be changed.

  • If you want to execute some code without creating an object

  • If you want to define a factory method.

  • If you want to return a static variable.

  • If the method is not using any instance variable.

  • If any operation is not dependent on instance creation

  • If there is some code that can easily be shared by all the instance methods, write that code into a static method.

  • If you are sure that the definition of the method will never be changed or overridden. As static methods cannot be overridden.


Rules of Static Methods in Java:

  • A static method belongs to the class rather than object of a class.

  • A static method can be invoked without the need for creating an instance of a class.

  • The static method can access static data members and can call static methods directly.

  • The static method cannot access non static data member or cannot call non-static method directly. An object is required to access non static members from a static method.

  • The this and super keywords cannot be used in static context.

  • The static methods can be called on an instance variable or on the class name. Recommended is to call on class name.


Example:


class StaticMethodDemo
{
String instanceVar= "Instance variable"; // instance variable
static String staticVar= "Static variable"; // static variable

//instance method
void instanceMethod()
{
System.out.println("Need an object to execute this");
}

//static method
static void staticMethod()
{
System.out.println("I want to execute this without creating an object");
//System.out.println(instanceVar);//cannot access instance variable directly
System.out.println(new StaticMethodDemo().instanceVar);//valid
System.out.println(staticVar); //access static variable directly
}
public static void main(String[] args)
{
StaticMethodDemo obj = new StaticMethodDemo();
staticMethod(); //call static method directly
//instanceMethod();//cannot call directly
obj.instanceMethod();//need an object
  }
}


Why instance variables cannot be accessed inside the static method directly?

The instance variables are the object level variables, instance variables are the part of object (instance variables get stored inside the object area in Heap memory) therefore instance variables get created only after object creation.

The static methods can be executed without creating any object means before creating instance variables. If you execute a static method which uses instance variables before creating an object then that means you are using instance variables even before creating them. To avoid this situation an object is must to use the instance variables.


Why this and super keywords cannot be used inside the static method?

The this and super keywords refer / point to an object.

The static methods can be executed without creating any object.

Consider, if you use this and super keywords inside a static method and then execute that static method on class name without creating an object then the value of this and super will be null. Therefore there is no use of this and super keywords without creating an object. To avoid this problem Java does not allow this and super keywords inside static method.

Share the article to help your friends