Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem understanding static inner classes

 
Jose Monzon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
I'm starting to prepare for the SCJP with the help of the Sierra & Bates book among others. And I would like to start participating in these forums and try to solve many of the doubts I have (and will have) during my studiying.

First is related to inner classes. I think I understand them, but there is a case where I don't quite get the logic behind. Let's see it with an example, suppose the following classes in a file called 'MyTest.java':




I would like to take the opportunity to congratulate this community for being such a great support to all the people who want to become better java programmers :-)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your logic is quite sensible, but unfortunately that's just not how it works. When you say something like

new A().new B()

it is actually translated into something like

new A.B(new A())

In other words, the instance of the outer class is used as an (extra, invisible) constructor argument for the inner class. A reference to the outer class argument is stored inside the inner class instance. Since a static inner class's constructor doesn't have this extra argument, nor the member variable to store the reference in, your code won't compile.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Your logic is quite sensible

Hmm.... when you say static .it is reated to class , not an object . so create an instance of static class not required to depends on outer class object.
 
Jose Monzon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest,

So, if I have understood correctly, Inner classes' constructors contains kind of a parameter that must be a reference of the outer class. something like:


I guess that if they had also an overloaded constructor with something like:



it would work as I expected

When the innerclass isn't static, I guess the "hidden" constructor's parameter is an object instead of a class, because in that case, you MUST have an object of the outer class in order to instantiate the inner class:




Do you know any resource where I can learn more about inner classes? I mean, where I can read about how are they managed by the compiler.

Thanks for your help!
 
Jose Monzon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Hmm.... when you say static .it is reated to class , not an object . so create an instance of static class not required to depends on outer class object.


"it is related to class, not an object".
I don't think this statement is correct. The class itself IS an object. For instance: Object.class <-- this IS an object, it's static and therefore it's equal to new Object().getClass(). Please, somebody corrects me if I'm wrong here.

To prove this, I can use reflection to inspect the XXXX.class object and get the static variables and methods:




Because an instance of my OuterClass IS-A OuterClass, I can access the static variables through an instance object, as you said, is not required, but I can perfectly do it because java is polymorphic.



My doubt was that if I can use objects to access static variables, why can't I use objects to create static classes in the same fashion? Ernest replied it's because the way those inner classes are created, but logic dictates that it should be possible.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic