Friday, February 25, 2011

Chapter 55: Static Inner Classes

In the chapter on Inner classes, I had promised that we will look in more detail about the different types of inner classes. Well here we are. This is the last and final type of inner classes. “Static” Inner Classes.

So, without any further delays, lets get Started!!!

Static Inner Classes

Actually static inner classes aren’t inner classes at all, by the standard definition of an inner class. While an inner class enjoys that special relationship with the outer class (or rather the instances of the two classes share a relationship), a static MyStaticExInnerClass class does not. It is simply a non-inner (also called “top-level”) class scoped within another. So with static classes it’s really more about name-space resolution than about an implicit relationship between the two classes.

A static MyStaticExInnerClass class is simply a class that’s a static member of the enclosing class:
class MyExOuter {
static class StaticInner { }
}

The class itself isn’t really “static”; there’s no such thing as a static class. The static modifier in this case says that the MyStaticExInnerClass class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.

Instantiating and Using Static Inner Classes

You use standard syntax to access a static MyStaticExInnerClass class from its enclosing class. The syntax for instantiating a static MyStaticExInnerClass class from a non-enclosing class is a little different from a normal inner class, and looks like this:

class MyExOuter {
static class MyStaticExInner {void do() { System.out.println("hi"); } }
}
class Test {
static class B2 {void goB2() { System.out.println("hi 2"); } }
public static void main(String[] args) {
MyExOuter.MyStaticExInner n = new MyExOuter.MyStaticExInner();
n.do();
B2 b2 = new B2();
b2.goB2();
}
}
Which produces
hi
hi 2

Exam Tip:
Just as a static method does not have access to the instance variables and nonstatic methods of the class, a static Inner class does not have access to the instance variables and nonstatic methods of the outer class. Look for static Inner classes with code that behaves like a nonstatic (regular inner) class. Be careful to spot them and you’ll be scoring points in the question without any trouble…

Previous Chapter: Chapter 54 - Anonymous Inner Classes

Next Chapter: Quick Recap - Inner classes

3 comments:

  1. There is no such thing like "static inner class" in Java. An inner class is always non-static. See JLS §8.1.3. You mean "nested class".

    ReplyDelete
  2. @Anon
    Thanks for pointing it out. Since the Static Nested Class is placed inside another class, technically they are inner classes too. Though the appropriate term might be Nested Classes, i used the term inner class for consistency and also because i was explaining the other types of inner classes .

    anyways - thanks for pointing it out. appreciate it...

    Anand.

    ReplyDelete
  3. An inner class is called inner class, because its objects can only exists "inside" other instances (the "outer" class). And that's not the case with your static inner classes. The Java Language Specification points that out clearly.

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers