• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Inner Classes

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing first. Credit goes to Velmurugan Periasamy and "the notes". Now I can offical steal the stuff ;-)
Here is the code:
//File name is Outter.java
public class Outter {
String name = "Outter Class";

public void print() {
System.out.println(name);
}
class Inner {
String name = "Inner Class";
public void print() {
System.out.println(name);
}
}
class InnerInner extends Inner {
public void print() {
System.out.println(name); //This is where the problem is
}
}
public static void main(String args[]) {
InnerInner inout = new Outter().new InnerInner();
inout.print();
}
}

*** Complier o/p: ****
Outter.java:19: Variable 'name' is inherited in inner class Outter. InnerInner, and hides a variable of the same name in class Outter. An explicit 'this' qualifier must be used to select the desired instance. System.out.println(name);
**** End of compiler o/p *****
I am unclear as to why an explict "this" is required.
Here is my reasoning:
- Because of inheritance from Inner, class InnerInner should see the variable "name" of Inner class. So when I use "name" in the print function of InnerInner class, should it not be clear that it means the "name" from Inner class? Outter variable "name" is hidden according to me in the InnerInner class because of the scope.
I understand that to access Outter variable "name" in InnerInner, I would have to use Outter.this.name.

Any help/clarification is much appreciated.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
InnerInner and Inner are both inner classes and the instance variable name of the enclosing class i.e Outer is visible for both inner classes. InnerInner extends Inner and therefore inherits the instance variable name from Inner class. Knowing that the compiler gets confused which one YOU mean to access.
here's another exemple:

I hope this helps.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InnerInner is an inner class inside Outer, so it can reach all fields of Outer; since it's a subclass of Inner, it can reach all non-private fields of Inner. So, I think there is no priority of being an inner class or subclass.
Well, in my opinion it's a tough question for Java compiler designers as well, this is the best way of doing it I think, just give an error and leave the decision to the programmer.
Serdar
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic