Skip to content
Advertisement

Instance variable of a static nested class vs static variable of an outer class

I was using a static nested class in java for a particular use-case. A minimal example of the same is shown below:

public class Foo {
    static int fooInner = getInner(); // CASE 1 

    private static class StaticFoo {
        int fooInner = getInner(); // CASE 2

        public int useFooInner(){
            System.out.println(fooInner);
            //do something
        }
    }
}

The question is how is the memory allocation in Case 1 different from that in case 2? Or is it the same? What if I make the case 2 variable static too. Will the memory usage differ?

NOTE: Please do not mention that shadowing will take place. Although I have put both the variables there, but it’s an “OR” case and that’s why the “CASE”s.

PS: I feel that the memory usage should be the same. Since the nested class is static, it won’t be created for every object and thus the instance variable fooInner (Case 2) will also be created just once. Thus, the getInner() function would run just once. But it is just at an abstract level + gut feeling. A more descriptive answer would be appreciated!

Advertisement

Answer

They are different.

From a memory allocation point of view, a static inner class is no different from a top level class. Your StaticFoo will be compiled to a class (Foo$StaticFoo.class) that is essentially independent from its parent class at runtime. At compile time, there are access checks for private members.

So, in case 1, you have a static field in a class. It will be allocated as a field on a Foo.class object on the heap. There will only be one instance per ClassLoader that loads the Foo class, which generally means just one shared instance for the whole JVM.

In case 2, you have an instance field in the Foo$StaticFoo class. On the heap, there will be space allocated (and a value assigned) for (and in) each instance of StaticFoo created. Each StaticFoo that gets created will access its own instance of that field, and since it’s not final, the value of each instance can be independently changed.

If you changed StaticFoo.fooInner to be static, then it would be exactly the same as case 1.

Note: The above is true only for Java 8 and later. For earlier JVMs, that amount of memory allocated in each case still matches the description above, but static variables, as well as being singletons per ClassLoader, are also stored in a different memory pool: PermGen Space rather than the main heap. See this answer for more details.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement