Why there is no static class in Java

前端 未结 5 815
死守一世寂寞
死守一世寂寞 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:51

    It is not a restriction, you do not need static class to define a utility class, you only need static methods. For example the class Math in java is full of static methods, but the class itself is not static.

    You might only need static class when you define an inner class that you want to use without creating an instance of the enclosing class, which is allowed in Java.

    You can define your utility class as follows:

    class Util {
      public static void method(){
         // your utility method
      }
    }
    

提交回复
热议问题