简体   繁体   中英

Why does Java allow nested class definitions in an interface?

I'm actually wondering on when a class in an interface would be beneficial from a OO/Acrhitectural/Structural point of view ?

Why does Java allow this ?

From what I remember in "Thinking in Java" B. Eckel showed it as relict which could be used to place in interface class with some static methods which could for example be tests of correct implementation of interface, for instance

interface SomeList<T>{
    T get(int i);
    void add(T t);
    int size();

    class FewTests{
        //some very bad test, but remember that this is just example
        static <T> boolean simpleTest1(SomeList<T> list, T t){
            int before = list.size();
            list.add(t);
            return list.size() - before == 1;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM