Java: what's the difference between an inner class and a static inner class?
Hi everyone,
I've read the java language specification 100 times but I still don't understand this detail. If I declare an inner class like this: Code:
public class Trie { This curiosity arose cause I'm using a code analyzer (findbugs) to get some more warnings and try to write cleaner code. It suggested making the inner class static to improve performance. (?) |
When you declare something, in Java, as "static" it is only created once, in the running virtual machine image.
For example - your inner class TrieNode. In your example it's a regular inner class. Every time a chunk of code within Trie creates a new TrieNode object, a new instance of that object is created. Now, should you declare it static (which I do not recommend, in your case)...well, to put it simply, that portion of your program won't work. Static declarations means it is - simply - created once, and only once; static classes cannot have a constructor. An example of a static class would be the java.lang.System class. Static declarations limit that item to only existing once; useful for things like enumerations and constants - data that may change but does not need to be re-made. And, unless I am mistaken, you do not actually declare a class static, just the elements within it. An example for where you might use a static declaration: Code:
[i]/* Constants and functions for doing calculations for CNC machining */[i] Code:
... |
Yes, I already knew about static members and static methods. In practice it means that a static member can be accessed just by the class name and no instance is required.
But now I realised that 'static' does not only apply to methods and members: an inner class can be declared static too. My program compiles and runs cleanly, but I don't know what changes. Anyway I looked at the java sources and System is not a static class, it's just a normal class with static methods. |
Quote:
In that case, I do not know what the difference would be. I do not know in down-'n-dirty info on how Java handles stacks and heaps, but I wonder if the difference is analogous to C++ where creating a new class by object or by reference to an object? (I do not know C++, so maybe I am just blowing wind, here.) EDIT: I found this link - http://www.javaworld.com/javaworld/j...a-static2.html - I don't know if it will help you, or not. |
The main difference between a static and non-static inner class is that a non-static inner class has a sort of secondary implicit "this" to an instance of the outer class and therefore can access member data of the outer class or call member methods. This also means that you cannot create a non-static inner class independently from an outer class, which can be problematic in some cases. I personally recommend that you always make an inner class static unless you must have access to the outer class' members.
|
Here is one of my early experiments. It contrasts two cases, one involving a non-static inner class and the other a static one. I hope it makes some sense.
Code:
class Outer{ |
Thanks a lot to everyone. It's quite clear now.
|
All times are GMT -5. The time now is 06:19 AM. |