Static Classes In Java

后端 未结 13 1741
慢半拍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:24

    Yes there is a static nested class in java. When you declare a nested class static, it automatically becomes a stand alone class which can be instantiated without having to instantiate the outer class it belongs to.

    Example:

    public class A
    {
    
     public static class B
     {
     }
    }
    

    Because class B is declared static you can explicitly instantiate as:

    B b = new B();
    

    Note if class B wasn't declared static to make it stand alone, an instance object call would've looked like this:

    A a= new A();
    B b = a.new B();
    

提交回复
热议问题