5

I have a couple of questions relating to nested classes within Java.

  • How do nested classes appear "under the hood", with regards to memory allocations?

  • You cannot declare a static variable within a nested class (I think the exact error was that static attributes can only be declared at the top level class). Why is this and what other restrictions are there for nested classes?

If possible, please say whether your answer is Java-specific, or whether C++ also follows the same rules?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
user997112
  • 29,025
  • 43
  • 182
  • 361
  • 2
    You are allowed to declare `static` variables in a nested class if that nested class itself is `static`. – Jeffrey Apr 07 '12 at 02:33

3 Answers3

2

Inner classes are exactly the same as regular classes as far as memory and compilation are concerned. (Perhaps there is some difference in the way they're managed in memory, but it's not visible to the average Java developer) In fact, when you compile a class that has an inner class, you can see the compiled .class file as OuterClass$InnerClass.class. So as far as I know, the JVM treats them the same.

As far as static variables are concerned, I'm not certain what the issue is. For example, this runs for me:

public class Tester {
  public static void main(String[] args) {
    System.out.println(InnerTester.MY_STRING);
  }

  public class InnerTester {
    public static final String MY_STRING = "MY_STRING";
  }
}

EDIT

As Jeffery pointed out, this does NOT compile:

public class Tester {
  public static void main(String[] args) {
    System.out.println(InnerTester.MY_STRING);
  }

  public class InnerTester {
    public static String MY_STRING = "MY_STRING";
  }
}

The difference is, I had the first static variable listed as final.

After thinking about this awhile, I agree with @Eugene. They allow static fields on inner classes to be final because then there's no problem with initialization. The value cannot change, you just initialize it to its value. If, however, the static field is not final, you then require an instance of the outer class to be created in order to initialize the field, and static members can't be tied to particular instances.

I found this discussion on it as well.

Community
  • 1
  • 1
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
  • 4
    [JLS §8.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3) Inner classes may not declare static members, unless they are constant variables (§4.12.4), or a compile-time error occurs. – Jeffrey Apr 07 '12 at 02:54
1

Basically a nested class is just a class with a field referencing the enclosing instance.

Unless it's a static nested class (inside which you can declare static fields/methods). In this case it's just a class with another path element in the name (package + enclosing class name).

In other words. Non-static nested class (inner class) - cannot exists without enclosing instance (e.g. page inside a book). And you instantiate as follows:

 Book book = ... // a book instance;
 Book.Page page = book.new Page(); // requires an enclosing instance

But the static nested class - is completely independent, it's like just a question of name space. You can instantiate it as:

 Outer.NestedStatic instance = new Outer.NestedStatic(); // just name-space

More information here: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
  • and if it's a static class, it's like delegation and non-static, like composition? – user997112 Apr 07 '12 at 02:48
  • If it's non static - it's composition. If it's static - it's just different name space - completely independent class. – Eugene Retunsky Apr 07 '12 at 02:49
  • Your terminology isn't correct. An inner class is a non-static nested class. So it can't be static. A nested class can be static. Only an inner class has the pointer to the outer instance. – user207421 Apr 07 '12 at 06:31
  • @EJP What is the difference between nested and inner? – user997112 Apr 07 '12 at 13:46
  • @user997112 A nested class is any class which is declared inside the body of another class. An inner class is a nested class which is not static and is not locally declared inside a method or expression. – Taymon Apr 07 '12 at 13:49
  • @Taymon, I presume when you say declared inside a method we're referring to anonymous classes? – user997112 Apr 07 '12 at 15:20
  • 1
    @user997112 we can declare a named class inside method. But it will be visible only inside this method (regardless it has name). – Eugene Retunsky Apr 07 '12 at 15:22
0

A nested class is simply the definition of a type inside another class. Java does not care in what scope a new type is defined, and you do not need to "handle" this inner class in any special way.

It wouldn't make sense to have a non-static inner class with static fields, since any reference to a non-static inner class is necessarily an instance variable. Only if the inner class is made static would it even be possible to statically assign it -- in this case, the static inner class is a standalone class, with the enclosing class name acting as yet another namespace level.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109