Static inner class?


I haven’t seen this before, and the description of it in the Java Tutorial is not very clear.

It means that the outer class RunDatabaseHelper does not own it, and you can’t access RunDatabaseHelper’s methods or variables. Does it create anything that is static between uses of it? Like, I am thinking that you want getRun() to return subsequent rows (or “run” items) in the table, so it needs to persist its location as you read it? How does it not just read the first run item from the run table each time?




What would be the difference with not making the inner classes used in this app static? Is it done just to prevent us from accessing non-static variables and methods of the parent class? Or is there another reason, like a performance one? Is this a general Java issue or a specific Android issue?



An inner class marked “static” is really an inner class in name only - in almost all respects, it acts exactly like a standalone class. This means that it has no implicit reference to an instance of the enclosing class.

The next natural question, then, is “Why use a static class instead of a standalone class?” They’re often handy for namespacing - i.e., where a class is really only of local interest, or even private.

And then the question on the other side - “Why not just use a regular inner class?” Inner classes, while handy, have a couple of drawbacks. They do not deserialize easily, and they can easily retain their enclosing class inadvertently. If you don’t have a good reason to use the magic that inner classes give you, it’s generally better to do without.