aspose file tools*
The moose likes Beginning Java and the fly likes Referring a Local Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referring a Local Inner Class" Watch "Referring a Local Inner Class" New topic
Author

Referring a Local Inner Class

Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
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

Joined: Sep 29, 2002
Posts: 1683
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.



SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Referring a Local Inner Class