Static Classes In Java

后端 未结 13 1844
慢半拍i
慢半拍i 2020-11-22 07:37

Is there anything like static class in java?

What is the meaning of such a class. Do all the methods of the static class need to be static

13条回答
  •  独厮守ぢ
    2020-11-22 08:12

    There is a static nested class, this [static nested] class does not need an instance of the enclosing class in order to be instantiated itself.

    These classes [static nested ones] can access only the static members of the enclosing class [since it does not have any reference to instances of the enclosing class...]

    code sample:

    public class Test { 
      class A { } 
      static class B { }
      public static void main(String[] args) { 
        /*will fail - compilation error, you need an instance of Test to instantiate A*/
        A a = new A(); 
        /*will compile successfully, not instance of Test is needed to instantiate B */
        B b = new B(); 
      }
    }
    

提交回复
热议问题