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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Difference between static and non-static method in Java
- Why non-static variable cannot be referenced from a static method in Java
- Static and non static blank final variables in Java
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static variables in Java
- Understanding storage of static methods and static variables in Java
- Internal static variable vs. External static variable with Examples in C
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | DoubleToIntFunction Interface in Java with Example
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Static Block and main() method in Java
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java