简体   繁体   English

java中的最终变量在哪里分配了内存?

[英]where is the final variable allocated memory in java?

if a final variable is declared in a class, and a number of instances of the class are created then where will the final variable be allocated memory? 如果在类中声明了最终变量,并且创建了该类的多个实例,那么将在哪里为最终变量分配内存? will it be present in all the instances or will be allocated separately independent of the instances? 它会出现在所有实例中还是会独立于实例单独分配?

When a variable is tagged with the final keyword, what you're saying is that it can be assigned once and only once. 当变量使用final关键字标记时,您要说的是只能分配一次。 It has nothing to do with the different instances of the class. 它与类的不同实例无关。 A variable like this: 像这样的变量:

final int myint = 0;

Will exist in every instance of the class, separately (each class instance will have its own instance of this variable. 将分别存在于该类的每个实例中(每个类实例将具有其自己的此变量实例。

You may be confusing this with the static keyword, which means that there will be one shared variable amongst all the instances of the class. 您可能将它与static关键字混淆了,这意味着在该类的所有实例之间将只有一个共享变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM