Abhishek Kumar
Abhishek Kumar

Reputation: 11

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?

Upvotes: 1

Views: 3004

Answers (1)

Kylar
Kylar

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

Related Questions