3

The Java Tutorial says that the static nested classes are accessed by using the name of the enclosing class like new EnclosingClassNameHere.StaticNestedClassNameHere()

Why would i want to create an instance of a static class at all? Can somebody please explain?

3 Answers 3

6

"static" in this case can be misleading. What it really means is that the class can exist independently. Non-static inner classes can't exist without an instance of the enclosing class.

IMO, when you start using an inner class outside the class that it's in, you need to consider moving it and making it its own top level class. There are very few cases where the relationship between the classes is so tightly coupled that you need to keep it as an inner class.

In your code example:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

You're creating a stand-alone instance of StaticNestedClass.

If it wasn't static, you couldn't do that. You could only create instances of StaticNestedClass() from an instance of OuterClass.

If you moved it to its own .java file, you could treat it nearly identically:

StaticNestedClass notNestedAnymore = new StaticNestedClass();

As to your real question: Why would you want to create an instance of it? For the same reason that you create instances of any class - it does some piece of work that you need.

Sign up to request clarification or add additional context in comments.

Comments

5

There is nothing confusing with this code. Static nested class is just a way to introduce yet another namespace.

By creating a static nested class you express very strong relationship between outer and inner class. Typically nested class is a helper or a part of the outer class. For instance when you create a Tree class, Node class is a good candidate for a nested static class. The Tree.Node clearly explains the purpose of the Node class.

In fact, static keyword usage is consistent with static fields. In both cases you can access static entity without an instance of enclosing class. When it comes to static classes it basically means: "I can create an instance of this static nested class without having an instance of outer class". By default (when static keyword is not used) the nested class becomes inner class. In this case you cannot simply write:

new OuterClass.StaticNestedClass();

Instead you are required to pass OuterClass instance with a bit obscure syntax:

OuterClass outerClassInstance = new OuterClass();
outerClassInstance.new InnerClass();

Fortunately when new InnerClass() is executed inside an OuterClass body, this is implictly used as enclosing instance.

3 Comments

i understand this part. i mean i can't figure out why do they create an instance of a static class in their example, which usually doesn't need an instance in C#: ClassName.memberName
@user1104888 Because you're misunderstanding the nature of word static in this context. You're thinking of static class variables--except here the class itself is static, which is orthogonal to creating instances of the class.
Thanks, now i see that i was misundertandint the word 'statis'. I used to think about it as about 'static' in C#, and therefore thought that the example above is meaningless. Unfortunately the tutorial doesn't have the most important sentence in that article - "I can create an instance of this static nested class without having an instance of outer class"
1

In java inner classes have an implicit reference to an instance of the outer class. This way you can access members of the outer class directly, which is usefull in annonymous classes used for callbacks.

class A{
   private int a = 3;
   class Inner{
      Inner(){

         System.out.println(a);//also A.this.a
      }
   }
   static class StaticInner{
       StaticInner(){
          System.out.println(a);//FAILS
       }

   }
}

Declaring an inner class static simply removes this implicit reference and that is the only difference between static and non static inner classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.