Static method in java

The Static method in Java is an important concept. But many programmers misunderstood and misused it.  In this post we will discuss the static method in java with example so that you can get is easy.  Almost all the programmer knows the static methods belong to the class and non-static methods belong to the objects of the class. but only a few of them understand what it means and when to use the static method and non-static method.

Here is the table content of the article we will cover this topic.
1. What is the static method in Java?
2. How to create a static method in java?
3. How to use a static method in java?
4. Scenario 1: When static variables and static methods are in the same class?
5. Scenario 2: When the static variable and static methods are in different classes?
6. Properties of Static Methods in Java?

What is the static method?

A method declared with a static keyword is known as a static method. The static method belongs to a class instead of instance/object. A static method is part of a class definition instead of the part of the object.

What is the mean of static method is part of class definition?

We can call a static method without the creation of an object. It means we do not need to create an object to invoke any static method. It means it’s not dependent upon the object state. In the other hand, the non-static invoked only through the objects. We can call them only when they are part of an object. That’s why the static method is part of the class definition.

How to create a static method?

To create a static method, we must use the static keyword with the method. Whenever we use the static keyword during the method declaration, the JVM binds that method with the class definition.

AccessModifier static returnType methodName(parameters)
{
     //Body of static method
}
static method in java

Here accessModifier specifies the scope of the method. You can read about access modifiers.
static is a keyword that is used to make it a static method.
returnType is the type of data that you want to return from the method.
methodName is the name of the method.
parameters, You can define any number of parameters.

How to use a static method?

We have read how we can create a static method. Now we will see how we can use it. As you know static methods are bind with class instead of the object. To use a static method, we don’t need to create an object we can access it by class name in which it is created.

className.methodName()
static method in java

Let’s see with examples, how we can create and use it. We will discuss it in different scenario that will clear some doubts.

Scenario 1: When static variables and static methods are in the same class?

Suppose we are working in a single class, here we have static variables and static methods. So, let us see how it will work in a single class.
A static method can access any static variable or static method without any object creation. As we have read already in the above section, to access a static variable or static method, we can use class names directly. But if the static variable and static method exist in the same class then we do not need to use the class name, Because of its presence in the same class. But to access a non-static variable or method we need to use the object.

class StaticMethodExample
{
	int a = 0;
	static int b = 0;
	
	void showData()
	{
		System.out.println("Value of a : "+ a);
		System.out.println("Value of b : "+ b);
	}
	
	static void displayData()
	{
		StaticMethodExample obj = new StaticMethodExample();
		System.out.println("Value of a : "+ obj.a);
		System.out.println("Value of b : "+ b);
	}
	
   
   public static void main(String arg[])
   {
	  displayData();
	  StaticMethodExample obj = new StaticMethodExample();
	  obj.showData();
   }
}

Output: Value of a : 0
Value of b : 0
Value of a : 0
Value of b : 0

static method in java

Scenario 2: When the static variable and static methods are in different classes?

Suppose we are working with multiple classes. We have static variables and static methods in different classes.  So, let us see how we can access them.
A static method can access any static variable or static method without any object creation. We can access them by use of the class names. But to access a non-static variable or method we need to use of the object.

class StaticExample
{
	int a = 0;
	static int b = 0;
	
	static String displayData()
	{
		return "Static method";
	}
}

public class MainClass
{   
   public static void main(String arg[])
   {
	   System.out.println("Value of b :"+ StaticExample.b);
	   System.out.println(StaticExample.displayData());
   }
}

Output: Value of b :0
Static method

Properties of Static Methods in Java

1. Restriction on this keyword

We can’t use this keyword within the static method because this keyword is a reference variable that is used to refer to the current object. We can’t use it in static method because static methods belong to class instead of an object If you try to use this keyword inside a static method then the compiler will show compile-time error in Java.

class StaticExample
{
	int a = 0;
	static int b = 0;
	static void initlize(int a, int b)
	{
		this.a = a;
		this.b = b;
	}
	void showData()
	{
		System.out.println("Values : "+ a + ", "+ b);
	}
	
	
    public static void main(String arg[])
    {
    	initlize(5, 6);
    	StaticExample obj = new StaticExample();
    	obj.showData();
    	System.out.println();
    }
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problems:   Cannot use this in a static context Cannot use this in a static context at StaticExample.initlize(StaticExample.java:9) at StaticExample.main(StaticExample.java:20)

2. Restriction on super keyword

We can’t use the super keyword within the static method because the super keyword is a reference variable that is used to refer to the current object. We can’t use it in static method because static methods belong to class instead of an object If you try to use the super keyword inside a static method then the compiler will show compile-time error in Java.

class ParentClass
{
	public void ParentData()
	{
		System.out.println("Method of Parent class");
	}
}
class ChildClass extends ParentClass
{
	public static void showData()
	{
		super.ParentData();
		System.out.println("Method of Child class");
	}
	
    public static void main(String arg[])
    {
    	showData();
    }
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem:     Cannot use super in a static context at ChildClass.showData(ChildClass.java:14) at ChildClass.main(ChildClass.java:20)

3. Can’t use non static variable in static method

We can’t use any non-static variable inside a static method. Because the non-static variable exists only when an object is created, and they don’t exist when the static method gets called. The static methods are not associated with any instance. In short, we can’t use non-static members inside your static methods in Java.

4. static method can’t be overridden

In Java, we can declare a static method in the child class with the same signatures as in Parent class. But it will not be considered the method overriding. The static method of child class will hide the implementation of the method of Parent class. It is known as method hiding. Because the compiler decides which method to execute at the compile-time, not at the runtime. As you know the static method can be accessed by class name rather than the object. When the compiler invokes the static method then it accesses the static method of the child class. You can read “Can we override the static method in java?

5. static method can be overloaded

Yes, we can overload the static method. A class can have more than one static method with the same name, but different in input parameters. You can overload the static method by changing the number of arguments or by changing the type of arguments. There are many ways to overload a method in Java. You can read “Can we overload static method in java?

6. static methods are faster than non-static

Yes, the invocation of static methods is more efficient than non-static methods. When the compiler invokes a non-static method, it needs to pass the current reference, but a static method can invoke without any reference.

7. Static method as Utility methods

The static method is useful to create Utility methods. Because utility methods are used to perform common and re-used tasks. So, we should make them static so that they can easily access by class name.

8. static method uses the static binding

In java static method bonds during the compile time. That is the main reason Java doesn’t support the overriding of the static method. Because the compiler decides which method to execute at the compile-time, not at the runtime. As you know the static method can be accessed by class name rather than the object. When the compiler invokes the static method then it accesses the static method of the child class. You can read “Static polymorphism or compile-time polymorphism?

9. static method can access by class name

It is the most powerful feature of the static method. We can call a static method by use of the class name and we don’t need to create an instance of that class.  The best example is the main method of the class. You know the main method is a static method so when JVM calls the main method, it doesn’t need to create an object of the class to call the main method.

10. static method supported in the interface

Since java 8, we can declare static methods in the interface. You can read “static method in interface

11. Invokes another static method and non-static method

 The static methods can invoke another static method by use of class name. But to invoke the non-static you have to create an object then you can invoke them.

Leave a Comment