• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Local Inner Classes

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic