为什么Java中没有静态类 [英] Why there is no static class in Java

查看:286
本文介绍了为什么Java中没有静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手。当我通过语言规范时,我发现无法声明静态类,但我们可以拥有静态内部类。我有点困惑为什么我们不能拥有顶级静态类。如果我想创建一个只包含实用程序方法的类,我可以去静态类。 Java开发人员限制此功能的原因是什么?

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 why we cannot have a top level static class. If I want to create a class containing only utility methods, I can go for static class. Any reason why developers of Java restricted this feature ?

推荐答案

这不是限制,你做需要静态类要定义实用程序类,只需要静态方法。例如,java中的类 Math 充满了静态方法,但类本身是而不是静态。

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.

在定义要使用的内部类时,可能只需要静态类,而不创建封闭类的实例,这在Java中是允许的。

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
  }
}

这篇关于为什么Java中没有静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆