Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Referring a Local Inner Class

 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

Here is a source code to understand the scope of a local inner class.

class A { protected char a = 'a';}
class B { protected char b = 'b';}

public class LocalClass extends A
{
private char c = 'c';
public static char d = 'd';
public void createLocalObject(final char e)
{
final char f = 'f';
int i = 0;
class Local extends B
{
Local L = new Local();
char g = 'g';


public void printVars()
{
System.out.println(g);
System.out.println(f);
System.out.println(e);
System.out.println(d);
System.out.println(c);
System.out.println(b);
System.out.println(a);
}
}
}
public static void main(String arg[])
{
LocalClass LC = new LocalClass();
LC.createLocalObject('e');
LocalClass.Local.printVars();
}
}




The compiler is not able to refer to "Local" in the last line of the main method.

LocalClass.Local.printVars();
^

Q1. What the mistake in the code..?
Q2. Is the char d accessible in Local, d being public static?


Regards,
Rekha
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1. What the mistake in the code..?

Because Local is not declared inside LocalClass but inside LocalClass.createLocalObject.
Now you can move Local outside of the method, but then it won't have access to variables e and f anymore.

Q2. Is the char d accessible in Local, d being public static?

Yes it is.

Also, printVars is not static so you'll need to create an instance of Local to be able to call it.
 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not want to move Local out of the enclosing method because then it will not remain "Local Inner Class" anymore..!

I have created an instance L of Local just after class definition. When I tried to call L.printVars() the compiler reported an error. That is why I tried calling it as LocalClass.Local.printVars() but got the same error. How can I refer to a Local Inner Class from the main()?

Regards,
Rekha
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of an inner class can exist only within an instance of its outer class. To instantiate an inner class, you must first instantiate the outer class. Then you create the inner object within the outer object like this.

 
Rekha Anand
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I read in a book that the Local inner class must be instantiated after its declaration and that too in the enclosing method. That is why I instantiated the inner class in the enclosing method and outer class in the main method, separately.

Regards,
Rekha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic