• 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

Method's Final variable is stored in stack/heap.

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to method-inner classes they can access only final variables of that method. Reason for this is all other variables are stored in stack and vanish after method is executed, But method inner class object can be assigned to public variable of top level class. so variables of stack should not be allowed.
If above statement is correct then it means FINAL local variables will be stored in heap. is it correct?
--------------
public class test1 //extends test3
{
public static void main(String args[])
{
new test1().add1(10);
System.out.println("FINAL local value "+mm);
}
void add1(int i){
final int mm = 100;
xx();
}
void xx()
{
System.out.println("Final local value "+mm);
}
}
in this example I declared mm as final in add1() method. But I am not able to access this method in child method xx() and parent method main(). It means Final local variable is like other local variables which will be stored in stack and vanish after method execution.

Method inner classes Final variable concept and this concept looking contradictory.???
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yamini,
I think I know where you're coming from ... the use of the modifier final in a method variable doesn't mean you can access that variable from outside the method. Method variables will die when the method ends, even if it is marked as final. That's not the reason why you're allowed to use final in method variables.
Consider the following:

Ok, what are we doing here. In method m1 we create an annonymous Object object ( ) and override the toString method. This toString method uses the local variable i. The object we create in line 1 needs to know what the value of i is to figure out what to do with its life in the toString method. You could think the actual toString method ends up looking like this when the object is created:
public String toString(){ //2
return "5"; //3
}
(I ignore if this is technically correct but it helped me understand these things)
So that's how the method in o looks like when we hit line 4. If you were allowed to now change i (if i was not final) the object o would not be aware of that change. It's going to print the value of i at the time the object was created and to guarantee that's still the value of i we must make i final.
Again, this is just my way of looking at it. Hope you find it useful.
cheers
[ April 20, 2004: Message edited by: Lionel Orellana ]
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very old thread...hopefully someone still has more on this...

similar thread here

Anyone to clarify?
reply
    Bookmark Topic Watch Topic
  • New Topic