java java中静态类只包含静态方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12365680/
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
Does static class contain only static method in java?
提问by user710818
I have found code in which static class contains methods that are not declared static. Compiler doesn't display any warning. It seems confusing, doesn't it?
我发现静态类包含未声明为静态的方法的代码。编译器不显示任何警告。看起来很混乱,不是吗?
回答by Nandkumar Tekale
There is no concept of static class in java (not even static inner class). If you see class is static and it is in working condition then it must be inner class(also called as nested class) which is declared as static. And there is no restriction to have only static methods in static inner classes.
java中没有静态类的概念(甚至没有静态内部类)。如果您看到类是静态的并且处于工作状态,那么它必须是声明为静态的内部类(也称为嵌套类)。并且没有限制在静态内部类中只有静态方法。
The significance of declaring an inner class static is to be able to create instances of the nested class independent of the class in which it is nested. If static is not mentioned then every instance of the nested class will be associated with an instance of the class in which it is nested.
将内部类声明为静态的意义在于能够创建独立于嵌套类的嵌套类的实例。如果未提及静态,则嵌套类的每个实例都将与嵌套类的实例相关联。
This question has more details. Java inner class and static nested class
这个问题有更多细节。 Java内部类和静态嵌套类
回答by Peter Lawrey
A static class is a nested class which has no implicit reference to the outer class. A static class can have static methods or instance methods. Note: an inner class can't have static methods.
静态类是嵌套类,它没有对外部类的隐式引用。静态类可以有静态方法或实例方法。注意:内部类不能有静态方法。
回答by noob
The answers here are good, but I guess I should add an example. Consider the following class definition
这里的答案很好,但我想我应该添加一个例子。考虑以下类定义
class Foo{
class Bar{
int x;
}
}
To use the class Bar you'll have to create an instance of class Foo first.
要使用 Bar 类,您必须首先创建 Foo 类的实例。
Foo foo = new Foo();
Foo.Bar bar = foo.new Bar();
bar.x = 10;
But if you have used the static keyword like the following
但是,如果您使用了如下所示的 static 关键字
class Foo{
static class Bar{
int x;
}
}
You could have skipped a step
你可以跳过一步
Foo.Bar bar = new Foo.Bar();
bar.x = 10;
If you'll try to statically use Foo.Bar directly when the inner class is not static, then you'll get the following error -
如果你尝试在内部类不是静态的时候直接静态地使用 Foo.Bar,那么你会得到以下错误——
error: an enclosing instance that contains Foo.Bar is required
Foo.Bar bar = new Foo.Bar();
^
1 error
I think this example should clear it.
我认为这个例子应该清楚它。
回答by Joachim Sauer
There's no such thing as a "static class" in Java (at least not in the way you seem to understand it). In C# there is a "static class" that can not be instantiated and can't have non-static members. That concept doesn't exist explicitly in Java (you can get the same effect by making all constructors private
, however).
Java 中没有“静态类”这样的东西(至少不是你理解的那样)。在 C# 中有一个“静态类”,它不能被实例化,也不能有非静态成员。这个概念在 Java 中并不显式存在(但是,您可以通过创建所有构造函数来获得相同的效果private
)。
What you're seeing is probably a static nestedclass like this:
你所看到的可能是一个像这样的静态嵌套类:
class Foo {
static class Bar {
}
}
This onlymeans that a Bar
instance does nothave a reference to an outer Foo
instance (as it would be the case if Bar
did not have the static
modifier).
这仅意味着一个Bar
实例时不具有外部参考Foo
实例(因为这将是情况下,如果Bar
没有足够的static
改性剂)。
Bar
can have any kind of members it could have if it were a top-level class.
Bar
如果它是顶级类,可以拥有它可以拥有的任何类型的成员。
回答by Mathias Schwarz
Only inner classes can be declared static
. A static class has no pointer to its outer class and can therefore only refer to static fields and methods of the outer class. A static class may however itself contain non-static methods.
只能声明内部类static
。静态类没有指向其外部类的指针,因此只能引用外部类的静态字段和方法。然而,静态类本身可能包含非静态方法。
回答by Adrien Clerc
The static
keyword is totally different for attributes/methods and classes.
static
属性/方法和类的关键字完全不同。
For attributes and methods, it will tell the compiler to have only one instance of the attribute or method that can be referenced without a living instance of the class.
对于属性和方法,它会告诉编译器只有一个属性或方法的实例可以在没有类的活实例的情况下被引用。
The static class
can only be used for inner class, to indicate that the class can be referenced without an existing instance of the enclosing class. It is therefore useful for inner enumeration that should be accessed from outside.
在static class
仅可用于内部类,以指示类可以没有封闭类的现有实例中引用。因此,它对于应该从外部访问的内部枚举很有用。
But a static class
can reference whatever it wants, from its own body.
但是 astatic class
可以从它自己的主体中引用它想要的任何东西。