1

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?

2
  • 6
    You're thinking of static variables; the final keyword is used only by the compiler, and it makes no difference to the JVM. Commented Sep 13, 2013 at 15:39
  • 1
    possible duplicate of stackoverflow.com/questions/12306234/… Commented Sep 13, 2013 at 15:40

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.