When to use static methods in java

In the recent post, we have read what is a static method in java. You must see there “how to create and use it in different scenarios”.  A static method has some unique properties. In this post, we will see “when to use static methods in java? or what is the need for static method? ”. To move further and for a deep dive you must know about the “Important properties of the static method”.

When to use static methods in java?

Let’s find out the answer to question “when to use static methods in java ?“. Making a method as the static method is an important part of your program or project. You need to take care of the properties of the static method also. Because a static method has some unique properties, if you want to use them you need to think about every perspective. Suppose you want to create an object to call a method, you want to restrict the overriding concept of method.  Here we have the number of scenarios we will discuss them one by one. That will help you to decide when to use static methods in java.

when to use static methods in java

Independent Code

The code which is written is a static method that is not dependent on object creation. As you know static methods do not bind with the object,  they are directly binding with class. So, the code is considered independent of object creation.

Suppose you have a record of School and you want to maintain all the Student’s data. Each student has some data like class name, roll no,  section, etc. that information exists based of per student in school. But the data of school like school name, location, etc. always exists which is not dependent on students.
If we talk about it in java, those method provides student information are only exists when an object for a will be created. But the information about the school is not dependent on object creation because it exists already.

class Student 
{
	String name;
	String className;
	int rollNo;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public Student(String name, String className, int rollNo) 
	{
		this.name = name;
		this.className = className;
		this.rollNo = rollNo;
	}
}

class Record
{
	public void StudentInfo(Student student)
	{
		System.out.println("Name : "+ student.getName());
		System.out.println("Class Name : "+ student.getClassName());
		System.out.println("RollNo : "+ student.getRollNo());
	}
	
	static void SchoolInfo()
	{
		System.out.println("School Name : ABCD");
		System.out.println("School Location : XYZ");
	}
}

public class MainClass
{
	public static void main(String args[])
	{
		Record record = new Record();
		// To get information of student must create an object
		record.StudentInfo(new Student("Ram", "MCA", 123));
		
		// To get information of school non need to create an object
		Record.SchoolInfo();
	}
}

Output: Name : Ram
Class Name : MCA
RollNo : 123
School Name : ABCD
School Location: XYZ

Invoke by Class Name

Many persons have a doubt why we use static method even we have a class with getters,  setter and we can create any number of non-static methods.  Why we should create a static method though we can invoke any non-static method by the object of the class.
The answer is simple,  sometimes we want to use the functionality of the method without the creation of objects.  So, the method must be static.

Let’s take a real time example. Each application has a class that contains main method.

public static void main(String args[])
{
     // Body of method
}

You have noticed the main method always a static method. Actually, the main method is the entry point of the execution of the class.  Whenever we run any application, the JVM finds the main method and invokes it by use of the class name. Suppose the main method will not be static then how JVM will create an object of class? Where it can store the created object? It creates a headache.

To write utility methods

If you want to create a utility method, then it would be great if you make them a static method. Utility methods are those methods that perform common and used functions. You must think why we should declare them as static?

The utility method provides the common functionality that you can use anywhere. If we make it a non-static method, then you must create an object to use it. Even you don’t want to construct the object of the class.
In our JDK there is the number of classes that have a utility method. Here we have class Math that has the number of static methods, the multiplyExact(int x, int y) is one of them.
To use the functionality of this method you just need to call it by class name because it is static.

Prevent the overriding the method

A static method can’t be overridden in child classes. So the time we don’t want to modify the implementation in child classes. 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 the article in detail “Can we override the static method in java”.

By keeping all the scenarios in mind, You can decide when to make a method static in Java and when to use them. 

Leave a Comment