简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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