This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes final variables in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final variables in method" Watch "final variables in method" New topic
Author

final variables in method

Shiva Shankar
Ranch Hand

Joined: Dec 07, 2006
Posts: 31
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

Joined: Aug 31, 2004
Posts: 11343

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?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3305
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 28, 2006
Posts: 236
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

Joined: Dec 07, 2006
Posts: 31
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Shiva Shankar
Ranch Hand

Joined: Dec 07, 2006
Posts: 31
what is synthetic field (of the inner class)?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
 
GeeCON Prague 2014
 
subject: final variables in method