Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Local Inner Classes

 
Prasanna Joshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is from Jaworski mock exam. I got this again today.
can anyone confirm my logic ?
Sankar
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic