• 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

Inner class Access Rights

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A member inner class can access the enclosing outerclass instance variables.
But the cant access the local variables of the enclosing method if they are not final.

To make things more clearer here is the example.

class innerClass
{
int instance_myInt=0;
public static void main(String args[])
{
System.out.println("I m in outerclassmain");
}

public int getInt(int local_myInt)
{
class nestedInner
{
public int innerInt()
{
instance_myInt+=1;
//local_myInt+=2; //Generates compile error
return 0;
}

}

return 0;
}
}//end of Class


My question is wot is the reason for such kinda of behaviour ? I mean wot advtg we get

Thanks in advance
Kanchan
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm not wrong it's because objects still remains in the heap even after the method finishes.

Think about it : How could an object in the heap hold a variable that vanished from stack when the method finishes ?

Maybe anyone else can explain better to you.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
im gonna try to help you to understand why you only can use in a method local class the final local variable...

you can't use a not-final marqued variable, because method's variable have a method scope, they 'live' in a stack. That means when the method is done they are gone...when they are final, they are stocked somewhere else ( i think on the heap).
Now why an method local inner class can't use not final var? look here :

whatwould happen if you could compile this...think about it lol
arno
[ April 06, 2006: Message edited by: Arno Reper ]
 
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic