0

Below class is a java class where i have seen static interface Inside this class what is the use of this static interface i have never seen and what advantages to create interface like this

public class Validator {
public static interface ItemValidator {
        public int withinTolerance(Number value, Number oldValue);
    }
}
Sadina Khatun
  • 1,136
  • 2
  • 18
  • 27

1 Answers1

-1

An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface. Since nested interface cannot be accessed directly, the main purpose of using them is to resolve the namespace by grouping related interfaces (or related interface and class) together. This way, we can only call the nested interface by using outer class or outer interface name followed by dot( . ), followed by the interface name.

Example: Entry interface inside Map interface is nested. Thus we access it by calling Map.Entry.

Note:

Nested interfaces are static by default. You don’t have to mark them static explicitly as it would be redundant. Nested interfaces declared inside class can take any access modifier, however nested interface declared inside interface is public implicitly. Example 1: Nested interface declared inside another interface

interface MyInterfaceA{  
    void display();  
    interface MyInterfaceB{  
        void myMethod();  
    }  
}  

    class NestedInterfaceDemo1 
        implements MyInterfaceA.MyInterfaceB{  
         public void myMethod(){
             System.out.println("Nested interface method");
         }  

         public static void main(String args[]){  
             MyInterfaceA.MyInterfaceB obj=
                     new NestedInterfaceDemo1(); 
          obj.myMethod();  
         }  
    }
rahul sharma
  • 101
  • 4
  • 10