Why does java have “static inner classes”? What is its use in the real world? what did the designer think when they added this feature?

Possible duplicate:
Are static inner classes a good idea or a bad design?

Java supports static inner classes, what should a language developer think when adding it, what problems does he solve in the real world?

+3
source share
4 answers

, , " " (.. ), " " , .. , , .

, AbstractMap SimpleEntry, . , , .

+12

. , "this" ( ).

, . private, . :

public class MyClass {

private static class MyInner {
    private void myMethod() {
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    System.gc();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    System.out.println("Inner class still alive! "+this);
                }
            }
        }).start();
    }
}

public MyClass() {
    new MyInner().myMethod();
}

protected void finalize() throws Throwable {
    System.out.println("MyClass has just been finalized");
    super.finalize();
}

public static void main(String[] args) {
    new MyClass();
}

}

: , , MyClass, MyInner!

. finalize() , , , .

+2

, "wrapper". , "".

+1

, , , .

0

Source: https://habr.com/ru/post/1767865/


All Articles