2

Is there a difference between initialisation of a static nested class and top level class?I understand that static class doesnt require an enclosing class instance but what happens if there are multiple instances of the static nested class? Just like static variables are shared by class instances, will the instances of the static class be shared also?

0

4 Answers 4

1

Let me see if I understand your question correctly.

A class can declare nested classes.

If a class C1 declares a non-static inner class C2, then C2 has access to all of C1's fields and methods, regardless of their access modifiers. C2 is, in fact, treated as a field: its declaration is loaded whenever a new instance of C1 is created. This means that non-static inner classes are rather more expensive than static ones, and should be avoided if not strictly necessary.

If a class C1 declares a static inner class C3, then C3 is shared across all instances of C1. It has access to all static methods and fields of C1, but not to non static ones - C3 is by definition not tied to a specific instance of C1, so there is nothing for it to have access to.

When you declare a static inner class, you're not saying anything about its instances. You're just telling the compiler that the class' definition is shared across all instances of the enclosing class. So, no, instances of the nested static class aren't shared automatically.

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

Comments

1

A static nested class does not required an instance of the enclosing class (as you point out) so there is nothing to share.

If you have a static variable then every instance of the class will hold a reference to the same static variable. Changes in one class will change the variable in all classes.

As a class is immutable at run time then this same logic doesn't carry through.

An instance of a static nested class is effectively the same as an instance of any other class.

The only way an instance would be shared would be if you had a static variable pointing to an instance of a static nested class. In this case it is the same as any other static variable.

As is pointed out the the tutorial the only real different between a static nested class and a top level class is that a static nested class can access private static members of it's enclosing class.

2 Comments

hi boris, thanks for your input "As a class is immutable at run time then this same logic doesn't carry through." any idea why this difference in behaviour?
What difference in behaviour? A class is a class and a variable is a variable. They are simply different.
0

Every class is a singleton object of type Class.

A static inner class is the base case. It is the other, normal inner class, that also has an OuterClass.this pointer for its instance objects.

So as such, there is no difference in class initialisation of any class.

Comments

0

The nested class as you declared it as "static class" does not differ from another top level class like inner class does. Adding the static to it declaration you promote it to be separated from owner class that became only a namespace for it.

package org.stack.question

public class Top {

  public static class Nested {

  }

}

To create a instance of Nested class you must only do this

Object instance = new org.stack.question.Top.Nested();

From specification:

Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language. Member interfaces (§8.5) are implicitly static so they are never considered to be 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.