I came across this in java, I know its a static nested class but why create an instance of 'static'. Isn't the whole idea of 'static' to use it without an instance? I understand the concept of inner classes, but why (and frankly how is it possible to) create an 'instance' of a 'static' member at all ?
Why this:
1- OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
2- nestedObject.aNestedClassMethod();
and not this:
1- OuterClass outerInstance=new OuterClass();
2- outerInstance.StaticNestedClass.aNestedClassMethod();
Use on inner classes, the keyword static indicates that you can access the inner class without an instance of the outer class.
For example:
with this construction, you can create an instance of the inner class by using this code:
If the inner class would not be static, but also like this:
Then, you would have to create an instance of Outer in order to access Inner: