Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods cannot be overridden or changed in the implementation class.
Similar to Default Method in Interface, the static method in an interface can be defined in the interface, but these methods cannot be overridden in Implementation Classes. To use a static method, Interface name should be instantiated with it, as it is a part of the Interface only.
Below programs illustrate static methods in interfaces:
Program 1: To demonstrate use of Static method in Interface.
In this program, a simple static method is defined and declared in an interface which is being called in the main() method of the Implementation Class InterfaceDemo. Unlike the default method, the static method defines in Interface hello(), cannot be overridden in implementing the class.
Hello, New Static Method Here Hello, Override Method here
Program 2: To demonstrate Scope of Static method.
In this program, the scope of the static method definition is within the interface only. If same name method is implemented in the implementation class then that method becomes a static member of that respective class.
Called from Interface PrintDemo Called from Class
- Understanding "static" in "public static void main" in Java
- Static Block and main() method in Java
- Static and non static blank final variables in Java
- static keyword in java
- Static import in Java
- Static blocks in Java
- Static class in Java
- Shadowing of static functions in Java
- Comparison of static keyword in C++ and Java
- Final static variable in Java
- Static Control Flow in Java
- Static vs Dynamic Binding in Java
- Output of Java Programs | Set 48 (Static keyword)
- Are static local variables allowed in Java?
- Can we Overload or Override static methods in java ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.