Static Classes In Java

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

    You cannot use the static keyword with a class unless it is an inner class. A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

    public class Outer {
       static class Nested_Demo {
          public void my_method() {
              System.out.println("This is my nested class");
          }
       }
    public static void main(String args[]) {
          Outer.Nested_Demo nested = new Outer.Nested_Demo();
          nested.my_method();
       }
    }
    

提交回复
热议问题