Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Problem understanding static inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem understanding static inner classes" Watch "Problem understanding static inner classes" New topic

Problem understanding static inner classes

Jose Monzon

Joined: Jun 05, 2008
Posts: 8
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 '':

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

Joined: Jul 08, 2003
Posts: 24183

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.

[Jess in Action][AskingGoodQuestions]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 05, 2008
Posts: 8
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

Joined: Jun 05, 2008
Posts: 8
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.

I agree. Here's the link:
subject: Problem understanding static inner classes
Similar Threads
Generics .. contravariance question on Collections.sort static method
How to call a generic method with two bounds (for a single argument) if one bound needs a cast
private final method
question from mughals book
Can a class be singleton after implements Runnable?