问题描述
通常,最终的静态成员,尤其是变量(或者当然,静态最终,它们可以按任意顺序使用而不重叠含义)广泛用于Java中的接口,以定义协议行为实现类,它意味着实现(继承)接口的类必须包含该接口的所有成员。
Generally, final static members especially, variables (or static final of course, they can be used in either order without overlapping the meaning) are extensively used with interfaces in Java to define a protocol behavior for the implementing class which implies that the class that implements (inherits) an interface must incorporate all of the members of that interface.
我无法区分最终和最终静态会员。最后的静态成员是一个声明为final或其他东西的静态成员?具体情况应该在哪些特定情况下使用?
I'm unable to differentiate between a final and a final static member. The final static member is the one which is a static member declared as final or something else? In which particular situations should they be used specifically?
静态变量或最终静态变量永远不能在方法既不在静态方法内也不在实例方法内。为什么?
A static variable or a final static variable can never be declared inside a method neither inside a static method nor inside an instance method. Why?
如果尝试编译它,编译时将不会编译以下代码段,并且编译器会发出编译时错误。
The following segment of code accordingly, will not be compiled and an compile-time error will be issued by the compiler, if an attempt is made to compile it.
public static void main(String args[])
{
final int a=0; //ok
int b=1; //ok
static int c=2; //wrong
final static int x=0; //wrong
}
推荐答案
你正在制作许多不同概念的巨大组合。甚至标题中的问题也与正文中的问题不符。
You are making a huge mix of many different concepts. Even the question in the title does not correspond to the question in the body.
无论如何,这些是你混淆的概念:
Anyways, these are the concepts you are mixing up:
- 变量
- 最终变量
- 字段
- 最终字段
- 静态字段
- 最终静态字段
- variables
- final variables
- fields
- final fields
- static fields
- final static fields
关键字 static
仅对字段有意义,但在您显示的代码中,您尝试在函数内使用它,在此函数中您无法声明字段(字段是成员)类;变量在方法中声明)。
The keyword static
makes sense only for fields, but in the code you show you are trying to use it inside a function, where you cannot declare fields (fields are members of classes; variables are declared in methods).
让我们试着快速描述它们。
Let's try to rapidly describe them.
-
变量在方法中声明,并用作某种可变本地存储(
int x; x = 5; x ++
)
variables are declared in methods, and used as some kind of mutable local storage (
int x; x = 5; x++
)
最终变量也在方法中声明,并用作不可变的本地存储( final int y; y = 0; y ++; //不会comp ILE
)。它们可用于捕获有人试图修改不应修改的内容的错误。我亲自制作了大部分本地变量和方法参数 final
。此外,当您从内部匿名类引用它们时,它们是必需的。在某些编程语言中,唯一的变量是不可变变量(在其他语言中,默认变量类型是不可变变量) - 作为练习,尝试弄清楚如何编写一个循环来运行初始化后不允许更改任何内容的指定次数! (例如,尝试使用最终变量来解决 fizzbuzz !)。
final variables are also declared in methods, and are used as an immutable local storage (final int y; y = 0; y++; // won't compile
). They are useful to catch bugs where someone would try to modify something that should not be modified. I personally make most of my local variables and methods parameters final
. Also, they are necessary when you reference them from inner, anonymous classes. In some programming languages, the only kind of variable is an immutable variable (in other languages, the "default" kind of variable is the immutable variable) -- as an exercise, try to figure out how to write a loop that would run an specified number of times when you are not allowed to change anything after initialization! (try, for example, to solve fizzbuzz with only final variables!).
字段定义对象的可变状态,并在类中声明( class x {int myField;}
)。
fields define the mutable state of objects, and are declared in classes (class x { int myField; }
).
最终字段定义对象的不可变状态,在类中声明,必须在构造函数完成之前初始化( class x {final int myField = 5;}
)。它们无法修改。它们在执行多线程时非常有用,因为它们具有与线程间共享对象相关的特殊属性(如果在构造函数完成后共享对象,则保证每个线程都能看到对象的最终字段的正确初始化值,并且即使它与数据竞争共享)。如果你想要另一个练习,尝试再次使用final字段再次解决 fizzbuzz ,没有其他字段,没有任何变量或方法参数(显然,你可以在构造函数中声明参数,但这就是全部! )。
final fields define the immutable state of objects, are declared in classes and must be initialized before the constructor finishes (class x { final int myField = 5; }
). They cannot be modified. They are very useful when doing multithreading, since they have special properties related to sharing objects among threads (you are guaranteed that every thread will see the correctly initialized value of an object's final fields, if the object is shared after the constructor has finished, and even if it is shared with data races). If you want another exercise, try to solve fizzbuzz again using only final fields, and no other fields, not any variables nor method parameters (obviously, you are allowed to declare parameters in constructors, but thats all!).
静态字段 在所有类的所有实例之间共享。您可以将它们视为某种全局可变存储( class x {static int globalField = 5;}
)。最简单(通常无用)的例子是计算对象的实例(即 class x {static int count = 0; x(){count ++;}}
,这里构造函数在每次调用时递增计数,即每次使用 new x()$创建
x
的实例时C $ C>)。请注意,与最终字段不同,它们本身并不是线程安全的;换句话说,如果你从不同的线程实例化,你肯定会使用上面的代码得到错误的 x
实例。为了使它正确,你必须添加一些同步机制或为此目的使用一些专门的类,但这是另一个问题(实际上,它可能是整本书的主题)。
static fields are shared among all instances of any class. You can think of them as some kind of global mutable storage (class x { static int globalField = 5; }
). The most trivial (and usually useless) example would be to count instances of an object (ie, class x { static int count = 0; x() { count++; } }
, here the constructor increments the count each time it is called, ie, each time you create an instance of x
with new x()
). Beware that, unlike final fields, they are not inherently thread-safe; in other words, you will most certainly get a wrong count of instances of x
with the code above if you are instantiating from different threads; to make it correct, you'd have to add some synchronization mechanism or use some specialized class for this purpose, but that is another question (actually, it might be the subject of a whole book).
最终静态字段是全局常量(类MyConstants {public static final double PI = 3.1415926535897932384626433;}
)。
还有许多其他细微特征(例如:编译器可以免费替换参考文献到最终的静态字段直接到它们的值,这使得反射在这些字段上无用;最终的字段实际上可能会被反射修改,但这很容易出错;等等),但我会说你还有很长的路要走在进一步深入挖掘之前。
There are many other subtle characteristics (like: compilers are free to replace references to a final static field to their values directly, which makes reflection useless on such fields; final fields might actually be modified with reflection, but this is very error prone; and so on), but I'd say you have a long way to go before digging in further.
最后,还有其他可能与字段一起使用的关键字,例如 transient
, volatile
和访问级别( public
, protected
, 私有
)。但这是另一个问题(实际上,如果您想询问它们,还有许多其他问题,我会说)。
Finally, there are also other keywords that might be used with fields, like transient
, volatile
and the access levels (public
, protected
, private
). But that is another question (actually, in case you want to ask about them, many other questions, I'd say).
这篇关于Java中静态变量和最终静态变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!