Reputation: 11
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?
Upvotes: 1
Views: 3004
Reputation: 9334
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.
Upvotes: 7