File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Local Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Local Inner Classes" Watch "Regarding Local Inner Classes" New topic
Author

Regarding Local Inner Classes

Prasanna Joshi
Greenhorn

Joined: Apr 25, 2000
Posts: 11
In one of the mocks/notes I came accross the sentence
"Local Inner Classes are not associated with an instance
of the outer (enclosing) class"
Could any one clarify the meaning of this??
Greg Belyea
Greenhorn

Joined: Feb 06, 2004
Posts: 29
yup, I'd say you misread the page...I would guess it said Static Inner classes are not associated with an enclosing instance of the Outer class. Local Inner Classes doesn't make sense...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
All local classes are also inner classes, and they definitely are associated with an instance of the enclosing class unless the local class is defined inside a static context - e.g. inside a static method, static field initializer, or static initializer. In these cases there is no instance which could be associated.


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I also came across this question: It was definitely:
"Local Inner Classes are not associated with an instance
of the outer (enclosing) class" - TRUE or FALSE.
Given answer : TRUE
I think it is from Khalid exam. Not sure.
What i understood was: we cannot reference a local inner class like: x.y
wehre x - is the instance of outer class and y is the instance of innerclass.
( Local instance class is like a local primitve variable and cannot be referenced by associating with an instance of the enclosing class).
Please correct me, if i am wrong.
cheers
Sankar
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It is from Jaworski mock exam. I got this again today.
can anyone confirm my logic ?
Sankar
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
A local inner class is (usually) associated with an instance of the outer class. See this example:
<code><pre>public class Outer {
public void show() {
System.out.println("show() in Outer");
}

public void method() {
System.out.println("method() in Outer");
class Local {
public void show() {
System.out.println("show() in Local");
}
public void method() {
System.out.println("\nmethod() in Local");
System.out.println("\ncalling this.show()");
this.show();
System.out.println("\ncalling Outer.this.show()");
Outer.this.show();
}
}
Local local = new Local();
local.method();
}

public static void main(String[] args) {
new Outer().method();
}
}</pre></code>

The line "Outer.this.show()" is possible because an inner class does have a reference to an instance of the outer enclosing class, which can be accessed using "[classname].this". The only time this isn't true is if the local class in defined inside a static context.
Sankar, your statements are true, but that's not what the Jaworski question actually said. The word "associated" is kind of vague in this case but I'm sure that if you can access an instance of the outer class, then that means an instance is somehow "associated" with the local class instance, and that means Jaworski's statement is FALSE.
 
 
subject: Regarding Local Inner Classes