What is static method in java?
- Details
- Written by Nam Ha Minh
- Last Updated on 25 August 2019   |   Print Email
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:
- static keyword in Java
- Differences between static and non-static stuffs in Java
- What is Nested Classes in Java? Static Nested Classes and Inner Classes
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand abstraction in Java
- Understand encapsulation in Java
- Understand inheritance in Java
- Understand polymorphism in Java
Comments