A Java static method belongs to the class and not to the object (instance). You have to use Static Keyword to create static methods in Java. A static method can access only static data.
Points to remember about the Static method in java:-
- A static method belongs to the class rather than an instance of a class.
- A static method invoked without the object of the class.
- The Static method can access static data members.
- It can be accessed just using the name of a class dot static name:-Employee.salary();
- It can’t call a non-static method from it.
- It can’t refer to “this” or “super” keywords in any way.
Java static method example
A static method can be accessed using the class name followed by a dot and the name of the method.
ClassName.methodName
Code:- We have a class “Languages” and static void “display” method. The void is used for no return value from a method. A display() will call from the main method without instance. In the next example, you will see how to call a static method from class.
class Languages { public static void main(String[] args) { display(); } static void display() { System.out.println("Java programming language."); } }
Output: Java programming language.
Java static method in interface
Maybe you’re thinking is it possible interface has a static method or not? So here is the answer- you can use a static method in the Java interface.
Same as using a static keyword in any class, you have to do it in the interface. As you know Java Interface doesn’t have a method definition (note: as of all versions below Java 8). And it can override in the implementation class.
But static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods can’t be overridden or changed in the implementation class.
interface ABInterface { static void hello() { System.out.println("Static Method Here"); } void overrideMethod(String str); } // Implementation Class public class InterfaceTest implements ABInterface { public static void main(String[] args) { InterfaceDemo interfaceDemo = new InterfaceDemo(); NewInterface.hello(); interfaceDemo.overrideMethod(Override Method here"); } @Override public void overrideMethod(String str) { System.out.println(str); } }
Output: Static Method Here
Override Method here
Q: What is the convention for naming static functions in Java?
Answer: Java methods should be named using camel cases and start with a small letter. And this is the same for static or non-static methods.
In order to name a static function wee follow <Acces Specififer> <Return type> <static> <function name><Parameter>.
Java static method naming conventions guidelines links:-
- Google style guide: https://google.github.io/styleguide/javaguide.html#s6.3-static-members
- Oracle: https://www.oracle.com/technetwork/java/codeconventions-135099.html
Q: When to use static methods?
Answer: First think about the method, what are the requirements of it. Then does this method make sense if called without an object? If so, it should definitely be a static method.
- Important and basic use of it to access static field(s) of the class.
- Or A code that needed to shared across all instances of the same class, then put that portion of code into a static method.
Q: How to call a static method in java?
Answer: call a static method using class name followed by a dot and the name of the method ClassName.method
. For an example of math class:-
MathUtils.add(50L, 20L); MathUtils.addInts(1, 2, 3);
Complete Example:- How to call the static method from another class. In this example, Example class calls a student class method “showData“.
public class Example { public static void main(String args[]) { Student.showData(); } } class Student { public static void showData() { System.out.println("Student data from student class"); } }
Output: Student data from student class
Do comment if you have any doubts and suggestions on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java static method call Example are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.