Java static method | Class, Interface, call Example

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.

Java static method

Points to remember about 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 member.
  • 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 anyway.

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.

Output: Java programming language.

Java static method in interface

May be you’re thinking is possible interface have a static method or not? So here is the answer- you can use a static method in Java interface.

Same as using a static keyword in any class, you have to do in interface. As you know Java Interface don’t have method definition (note: as of all versions below Java 8). And it can override in the implementation class.

But a 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.

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:-

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 static method.

Q: How to call 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 example of math class:-

Complete Example:- How to call the static method from another class. In this example Example class calling a student class method “showData“.

Output: Student data from student class

Do comment if you have any doubt and suggestion 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.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.