• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

final variables in method

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Enthuware Software Support
Posts: 4851
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is synthetic field (of the inner class)?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic