为什么不能将 Java 类声明为静态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2376938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't a Java class be declared as static?
提问by gmhk
I am trying to find why the class cant be created as a static? Like:
我试图找出为什么不能将类创建为静态的?喜欢:
public static class Qwert{
public static void main(String args[]){
int x = 12;
while(x<12){
x--;
}
System.out.println(" the X value is : "+ x);
}
}
采纳答案by Kevin Conner
In Java, the static
keyword typically flags a method or field as existing not once per instance of a class, but once ever. A class exists once anyway so in effect, all classes are "static" in this way and all objects are instances of classes.
在 Java 中,该static
关键字通常将方法或字段标记为存在,而不是每个类的每个实例都存在一次,而是一次。一个类无论如何都存在一次,所以实际上,所有类都是“静态”的,所有对象都是类的实例。
static
does have a meaning for innerclasses, which is entirely different: Usually an inner class instance can access the members of an outer class instance that it's tied to, but if the inner class is static
, it does not have such a reference and can be instantiated without an instance of the outer class. Maybe you saw that someplace, then tried to use it on a top-level class, where it isn't meaningful.
static
确实对内部类有意义,这是完全不同的:通常内部类实例可以访问与其绑定的外部类实例的成员,但如果内部类是static
,则它没有这样的引用并且可以实例化没有外部类的实例。也许你在某个地方看到过,然后试图在顶级类中使用它,但它没有意义。
Or maybe you saw it in other languages like C#, whose syntax is an awful lot like Java's.
或者你可能在其他语言中看到过它,比如 C#,它的语法非常像 Java。
(One time I couldn't figure out why an outer class instance wasn't being garbage-collected -- it was because I was keeping a reference to one of its inner class instances elsewhere, and the inner class was not static
and so had a reference to the outer class instance. So by default, I make inner classes static
now.)
(有一次我不明白为什么一个外部类实例没有被垃圾收集——那是因为我在别处保留了对其内部类实例之一的引用,而内部类没有static
,所以有一个对外部类实例的引用。所以默认情况下,我static
现在创建内部类。)
回答by edwardsmatt
To prevent a particular class being instantiated you should add a private Constructor. This stops 'any other' Class from being able to create an object of type Qwert
.
为了防止特定类被实例化,您应该添加一个私有构造函数。这阻止了“任何其他”类能够创建类型的对象Qwert
。
for example:
例如:
public static class Qwert{
private Qwert() {}
public static void main(String args[]){
int x = 12;
while(x<12){
x--;
}
System.out.println(" the X value is : "+ x);
}
}
回答by charlie
To prevent any class from creating an instance of Qwert
, either by inheritanceor by using reflection, you make the constructor fail by placing a poison pill:
为了防止任何类Qwert
通过继承或使用反射来创建 的实例,您可以通过放置毒丸使构造函数失败:
public class Qwert {
private Qwert() throws IllegalAccessException {
throw new IllegalAccessException("Utility class!");
}
public static class Yuiop {
public Yuiop() throws IllegalAccessException {
// generates a synthetic accessor method to super()
}
}
public static void main(String args[]) {
new Yuiop();
}
}
回答by Amit Bhandari
its because when we use static keyword for a component, that component becomes a class level component and its memory is taken by its class.
这是因为当我们对组件使用 static 关键字时,该组件将成为类级别的组件,并且其内存被其类占用。
回答by Naresh Joshi
We should define members as static which
我们应该将成员定义为静态的
- Should be common to all objects of the class.
- Should belong to the class and accessible by class name.
- Should not need an object of class to access them.
- 应该对类的所有对象通用。
- 应该属于类并且可以通过类名访问。
- 应该不需要类的对象来访问它们。
Now suppose we are defining an outer class as static and suppose we are allowed to do so. Will this serve any purpose or provide any advantage to a developer or it will create ambiguity and complications for both developers and language creators?
现在假设我们将外部类定义为静态并假设我们可以这样做。这对开发人员有任何用途或有任何好处,还是会给开发人员和语言创建者带来歧义和复杂性?
Let's check, defining an outer class as static will serve purposes which we have defined above or not?
让我们检查一下,将外部类定义为静态是否可以达到我们上面定义的目的?
- Every class is already common to all of its objects and there is no need to make it static to become available to all of its objects.
- We need a class name to access its static members because these members are part of class while an outer class is part of package and we can directly access the class by just writing package_name.class_name (similar to class_name.static_field_name), So again there is no need to do which is already there by default.
- We do not need any object to access a class if it is visible, we can simply write package_name.class_name to access it. And by definition, a class is a blueprint for its objects and we create a class to create objects from it (exception will always be there e.g. java.lang.Math), again there is no need to define an outer class as static.
- 每个类对于它的所有对象已经是通用的,并且没有必要使它成为静态的以使其所有对象都可用。
- 我们需要一个类名来访问它的静态成员,因为这些成员是类的一部分,而外部类是包的一部分,我们可以通过编写 package_name.class_name(类似于 class_name.static_field_name)直接访问类,所以再次有无需执行默认情况下已经存在的操作。
- 如果一个类是可见的,我们不需要任何对象来访问它,我们可以简单地编写 package_name.class_name 来访问它。根据定义,一个类是它的对象的蓝图,我们创建一个类来从中创建对象(例外总是存在,例如 java.lang.Math),同样没有必要将外部类定义为静态的。
From above points, we can say Java creators had not allowed an outer class to be static because there is no need to make it static. Allowing to make the outer class static will only increase complications, ambiguity and duplicity. Read more on Why An Outer Java Class Can't Be Static
从以上几点,我们可以说 Java 创建者不允许外部类是静态的,因为没有必要将其设为静态。允许使外部类静态只会增加复杂性、歧义和口是心非。阅读更多关于为什么外部 Java 类不能是静态的