Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

automatic variables availability?

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are automatic variables never available to an inner class that has been defined in a method of the enclosing class?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example...
public class TestClass
{
static int si = 10; int ii = 20;
public void inner()
{
int ai = 30; //automatic variable
final int fai = 40; //automatic final variable
class Inner
{
public Inner() { System.out.println(si+" "+ii+" "+fai); }
}
new Inner();
}
public static void main(String[] args){ new TestClass().inner(); }
...
automatic variable is never available... even if inner was a static method?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, only final local variables are available to a local class.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so an inner class can gain access to final method-local variables, and can gain access to the enclosing static and non-static class-member variables..
hmmm, i start to get confused by all these rules - still, i guess it becomes logical after a while, when you start understanding how everything locks together because of relationships, and the way things work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic