Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variables in method

 
Shiva Shankar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A method can have final and non-final variables in java.
1.Where these(final&non-final) variables are stored in JVM?
2.what is the life time of final variables of a method?
3.Can we access final variables of a method out side of that method, if so how? Give me example and detailed explanation.

Wating for your answers....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Shankar:
... Wating for your answers....

I'll wait with you.

Hey, while we're waiting, do you have any ideas about what the answers might be?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3742
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

I'll wait with you.




Shiva,
Try this : http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does not look like something from the SCJP objectives to me. Let's stack it on one of our Java In General forums...

No?
[ February 01, 2007: Message edited by: Barry Gaunt ]
 
Sanjit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys last two questions really don't fit for this category(SCJP) but the first one is really nice and it would be nice if you guys please explain "how JVM treats (regarding storing in memory etc..) final and non-final variables(local or instance).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Does not look like something from the SCJP objectives to me. Let's stack it on one of our Java In General forums...

No? ...

Yes. Moving to JIG Beginners.
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

non final variables of a method are stored in stack

and final variables in the method where also stored in stack

but having some indication(OPTIMISATION) that it is not going

to change in future

regards
amir
 
Shiva Shankar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the final variables are also stored in stack,how can method local inner classes access only final variables of that method,why not non-final variables?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Shankar:
if the final variables are also stored in stack,how can method local inner classes access only final variables of that method,why not non-final variables?


The problem is that the life time of the local variable might be shorter than that of the instance of the inner class. The compiler uses a trick to create the illusion of accessing the local variable even after its lifetime is over - it copies its value to a synthetic field of the inner class when it is instanciated. The local variable needs to be final to make sure that its value cannot get inconsistent with the copy in the synthetic field.
 
Shiva Shankar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is synthetic field (of the inner class)?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Shankar:
what is synthetic field (of the inner class)?


A synthetic field is a field that is introduced by the compiler and only exists in the byte code, not in the source code. I think there even is a synthetic flag in the byte code that is set for those fields, but I'm not sure about the details.

Synthetic members are necessary because at the byte code level, there are only top level classes - the JVM doesn't know about inner, nested or anonymous classes. The compiler has to use some tricks to implement the special access rules that apply to those concepts at the source code level.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic