This article helps you understand what is static method in Java with code examples.

Basically, a static method is the one that declared with the static modifier, for example:

public class A {

	static void doSomething() {
		// this is a static method
	}
	
	void doOtherThing() {
		// this is a non-static method
	}
}

In this example, the class A has a static method doSomething() and a non-static method doOtherThing(). In object-oriented programming, we usually create an object (instance) of a class and invoke its methods like this:

A a1 = new A();
a1.doOtherThing();

But a static method doesn’t associate with any instances of a class, so we can invoke a static method without creating a new instance of the class, for example:

A.doSomething();

Here, you can use the class name followed by a dot and then access its static method or variable. A static method belongs to the class itself so a static method is also called class method.

And since a static method doesn’t belong to any instances of the class, it cannot access non-static methods or non-static variables of the class. Because of this, beginners often make mistake of using non-static variables from a static method, causing a compile error like this:

non-static variable xxx cannot be referenced from a static context

The following code illustrates this point:

public class A {
	
	int x; // non-static variable
	
	static int y;	// static variable

	static void doSomething() {
		y = 10; // valid, since y is static
		
		x = 5; // ERROR: x is non-static
	}

	void doOtherThing() {
		x = 10; // valid
		y = 5;	// valid 
	}
}

Do you know a very well-known static method in Java? Yes, you may guess it right – the main() method:

public static void main(String[] args) {

}

The main() method must be static so the Java Virtual Machine can invoke its without create an instance of the class, to run a Java program.

 

When to use static methods in Java?

If a method doesn’t use any instance members (non-static methods and non-static variables) of the class, you can consider make it as static. In real world, static methods can be:

  • Utility methods which are pure functions with procedural code. You can see many static methods in JDK classes like Arrays and Collections.
  • Factory methods that are used to create objects. You can see examples of static factory methods in JDK classes like Files and Paths (in java.nio package).

 

Static method and Overriding in Java

A static method cannot be overridden, though it’s possible to have the following code:

public class Parent {
    protected void foo() {
        
    }
}
 
public class Child extends Parent {
    public static void foo() {
        
    }
}

Here, the foo() method of the Child class hides the one in the super class, not overrides it.

 

Related Tutorials:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment