Java nested classes – Static vs Non-static

I was working on a client project where I’m utilizing SugarORM as the ORM of the choice. One of the POJOs had few nested classes that I wanted to cache and query using SugarORM.

The SugarORM methods to perform standard CRUD operations on the objects were not working. Through some Googling, I realized that you had to make the nested classes static classes in order for the SugarORM methods to work. So to give a quick example

public class Foo extends SugarRecord {
    public Foo() {}
    public class NestedFoo extends SugarRecord {
        public NestedFoo() {}
    }
}

In the above example, SugarORM methods will work for Foo, but won’t work for NestedFoo. To make the SugarORM work for NestedFoo, we need to make the NestedFoo into a static class, like this.

public class Foo extends SugarRecord {
    public Foo() {}
    public static class NestedFoo extends SugarRecord {
        public NestedFoo() {}
    }
}

This brings up the question, what is the difference between static nested classes vs non-static nested classes in Java?

In Java, nested classes are divided into two categories: Static and Non-Static. Non-static nested classes are also simply called inner classes. These inner classes can only exist within an instance of the outer class. So for example, if the NestedFoo class above was non-static, that NestedFoo class will only be able to exist within an instance of the Foo class. Inner classes also have full access to all methods within the instance of the >Foo class.

Static nested classes do not have access to the instance of its parent class. So in our example, if the NestedFoo was a static class, it would not have access to the non-static methods or even an instance of the parent Foo class. In fact, nested static classes, if one wants to instantiate them separately, would be instantiated like this

Foo.NestedFoo nestedFoo = new Foo.NestedFoo();

while non-static nested classes would be instantiated like this

Foo foo = new Foo();
Foo.NestedFoo nestedFoo = foo.new NestedFoo();

Now back to the topic of SugarORM. For nested classes, why does SugarORM only work with static classes? To be honest, I have no idea. I’ll probably have to dig into the source code of SugarORM to be absolutely sure, but my guess at this point is the following:

  1. If the nested class is a normal inner class and non-static, then it can only exist in the presence of an instance of the outer Foo class. The outer Foo class already extends from SugarORM in my case, so something could be conflicting there.
  2. Again, if the nested class is non-static, it can only exist within the instance of the outer Foo class. It also has access to all of the non-static methods to the instance of the outer Foo class, so in a way, it’s kind of like extending a class. I wonder if something can potentially conflict if you extend an inner class.
  3. From learning about the primary differences between a static vs non-static nested classes, it seems like static nested classes really are just normal classes excepted nested. One advantage of this is that the nested static classes do have access to static methods in the outer parent class, and if you only use this nested class within the context of the parent class, then it’s a much better way of organizing your code. And because the only relationship between the parent class and the nested class is that the nested class has access to static methods in the parent outer class, then extending the nested class should have no ill-side-effects compared to extending a nested non-static class, therefore allowing SugarORM library to do its magic.

Again, the above are just educated guesses. Maybe one day I’ll dig into the source code of SugarORM to find out for sure, but for now, the above will do for me.

About the Author Chris Jeon

Software developer currently focusing on Android development.