Why there is no static class in Java

前端 未结 5 817
死守一世寂寞
死守一世寂寞 2021-01-14 16:52

I am new to java. When I was going through language specification I found that static classes cannot be declared, but we can have static inner classes. I am little confused

5条回答
  •  臣服心动
    2021-01-14 17:54

    static is a relative term.

    static means "independent of the containing instance". So a static field has the same value, independent of the instance of the class. A static inner class is valid for every instance of the parent class.

    But what would a static top level class be "independent of the containing instance" of? There is no containing instance for a top level class. That's why it cannot be static (or, it always is static, depending on your point of view - but in either way, no need to specify it).

    Implementation wise, a non-static inner class contains a reference to the containing outer class. Obviously this difference also is not possible for top level classes.

提交回复
热议问题