• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How is final reference variable is handled?

 
Greenhorn
Posts: 10
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that for final primitive types one can not change the value and for final reference variable, one can not change the reference, but can change the state of the object. I also understand that final primitive types are compile type constant, means they are replaced by their value at compile time.

My question is how the final reference variable are allowed to change their state? If they are compile constant, then how one can change the value at run time?

May be I am not able to frame the question properly.

Any help would be highly appreciated.

Thank you
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final references cannot be changed to point to a different instance. But you can do a- finalReferenceVariable.setSomeValue and change the value of some instance variable of that instance. They are constants because they cannot be made to point to a new instance or a different instance but changing the state doesnt mean the reference variable changes.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When reference which is constant (final) points to an object, then it means it is pointing to a Heap Space where the object was created.
Final means reference cannot point to some other memory space, but the instance variable residing at that space can be changed.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mittal vishal wrote: . . . Final means reference cannot point to some other memory space, but the instance variable residing at that space can be changed.

That is confusing. What does it mean?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankush Kanungo wrote:My question is how the final reference variable are allowed to change their state?



That's correct, you have not framed the question properly.

A variable does not have a "state". A variable contains a reference to an object, and it's the object which has a "state". Don't confuse a variable with an object which it refers to. (This is a common point of confusion.)

So when you declare a variable "final", that means that you assign it a reference to an object at that point. You can never assign any other reference to that variable.

However you can certainly change the state of that object. There is no way for the JVM to determine whether there are any final variables referring to the object, anyway.
 
Mittal Vishal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

mittal vishal wrote: . . . Final means reference cannot point to some other memory space, but the instance variable residing at that space can be changed.

That is confusing. What does it mean?



Every reference point to a memory space in heap. When any object is created, it is created on heap & the reference points to that memory space.
Step1) Object obj1 = new Object();
It means new objects is created on heap at some memory space. obj1 is pointing to that memory space
Now, if it is not declared as final then
Step2) Object obj2 = new Object();
Step3) obj1 =obj2; is a valid statement i.e. obj1 is now pointing to memory space where obj2 is pointing

but this is not a valid statement if obj1 was declared as final i.e. it is not allowed to point to some other memory space if it has already assigned a memory space.
In our case at step 1 it is pointing to a memory space so after that it cannot point to some other place
 
Ankush Kanungo
Greenhorn
Posts: 10
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you every one for their reply.

After doing some research and writing some sample code. It seems that Java uses some mechanism which does not allow final object to point to some other object at compile time. People who are saying about heap and all, I think, it all happens at runtime, if you have declared a reference to an object and trying to change the reference, the code will not even compile.

Please correct me if I am wrong.

Thank you



 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explaining what you meant, Mittal Vishal. I think final variables and fields are handled at compile-time. You can test this by compiling a little class like thisCompile and display the bytecode like this . . . and see whether there is any difference in the bytecode if you omit the two final keywords.
 
Ankush Kanungo
Greenhorn
Posts: 10
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

You are absolutely right. Final variables are compile time constant, i.e. their values are finalized at compile time and if you change the value, you need to again compiler all the classes where ever it is used in order to see the changed value. After doing some research, enums are an alternative to using final, because they get their value at run time.

Thank you all for their replies!!
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mmmm, some final variables are compile-time constants. Definitely not all of them are, however. In order to be a compile-time constant, a variable must be final, and the type of the variable must be a primitive or String, and the variable must be initialized at declaration using an expression which is itself a compile-time constant expression. The rules for what is a constant expression are somewhat longer - see them here.

For example, the following are not compile-time constants:
 
reply
    Bookmark Topic Watch Topic
  • New Topic