java中的静态类和最终类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19151911/
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
Static classes and final classes in java
提问by gnuanu
In Java (and in Android), what's the use of static class
and final class
declarations?
在 Java(和 Android)中,声明static class
和final class
声明有什么用?
My question is not about static instances but class declarations like,
我的问题不是关于静态实例,而是关于类声明,例如,
static class StaticClass {
//variables and methods
}
and
和
final class FinalClass {
//variables and methods
}
Thanks,
谢谢,
采纳答案by Jabir
Static Nested Classes
静态嵌套类
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
与类方法和变量一样,静态嵌套类与其外部类相关联。和静态类方法一样,静态嵌套类不能直接引用在其封闭类中定义的实例变量或方法:它只能通过对象引用来使用它们。注意:静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样。实际上,静态嵌套类在行为上是一个顶层类,为了方便打包,它嵌套在另一个顶层类中。
Static nested classes are accessed using the enclosing class name:
使用封闭类名访问静态嵌套类:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
例如,要为静态嵌套类创建对象,请使用以下语法:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Final Classes
期末班
A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.
声明为 final 的类不能被子类化。这尤其有用,例如,在创建像 String 类这样的不可变类时。
http://docs.oracle.com/javase/tutorial/java/IandI/final.html
http://docs.oracle.com/javase/tutorial/java/IandI/final.html
回答by SpringLearner
if you make a class as final then it can not be inherited. Generally top level class can not be made static however inner class can be made as static and Nested static class doesn't need reference of Outer class staticand finalclass in java
如果你把一个类设为 final,那么它就不能被继承。通常顶级类不能设为静态,但内部类可以设为静态,嵌套静态类不需要在 Java 中引用外部类 静态和最终类
回答by Siva
final classeswill restrict for further extends (Inherit).
final 类将限制进一步扩展(继承)。
You can not use static keyword on outer class,static is permitted only to inner classes Static classes
不能在外部类上使用 static 关键字,静态只允许用于内部类静态类
回答by Alexander Kulyakhtin
You make a class final so it can not be extended. Usually it makes sense if you'are creating a library (or working on a part of a big project), so your clients are not able to extend the class and modify the existing behavior. In your own program there's little reason to make a class final unless it's a big program and you can inadvertently forget things.
您将课程设置为 final,因此无法扩展。如果您正在创建一个库(或在一个大项目的一部分上工作),通常这是有意义的,因此您的客户无法扩展该类并修改现有行为。在你自己的程序中,没有什么理由让一个班级进入决赛,除非它是一个大程序,你可能会不经意间忘记一些事情。
Static inner classes are for things that logically belongs to an outer (containing) class but which have no dependencies on the state of the outer class. For example you can have a Parser class and an inner Parser.Listener class. Normally if you decide to have an inner class try, first, to make it static, if possible, to simplify things.
静态内部类用于逻辑上属于外部(包含)类但不依赖于外部类的状态的事物。例如,您可以有一个 Parser 类和一个内部 Parser.Listener 类。通常,如果您决定使用内部类,请首先尝试使其静态化,如果可能的话,以简化事情。
You could do without both final and static inner classes then with experience you will find use for them.
你可以不用 final 和 static 内部类,然后根据经验你会发现它们有用。
回答by Chirag Kathiriya
other class can not extends the final class exmple String is final class so you cannot extends this class. You cannot have a 'top-level' class declared static. You can only have an inner class with the modifier 'static'. static inner class only access the static member of the outer class
其他类不能扩展最终类,例如字符串是最终类,因此您不能扩展此类。您不能将“顶级”类声明为静态。您只能拥有带有修饰符“static”的内部类。静态内部类只访问外部类的静态成员
回答by Pankaj Sharma
you can't mark a class static ? yes, if it is nested class no, if it is normal class. if you mark a nested class static it will work as fully flagged class just use a classname.staticClassNmae to access it.
你不能标记一个类是静态的吗?是的,如果是嵌套类,则否,如果是普通类。如果您将嵌套类标记为静态,它将作为完全标记的类工作,只需使用 classname.staticClassNmae 即可访问它。
if you mark a class final , it can't be inherited , a good example of this String class in java.
如果你将一个类标记为 final ,它就不能被继承,这是 java中这个String类的一个很好的例子。