Question

In Java a nested class is an inner class that is declared static. E.g.:

class Basic{

   public static class NestedClass{};
}

I am wondering if a nested class is a singleton by default, or if I may create a list of instances such as

class Basic{

   public static NestedClass{};

   List<NestedClass> items;
}
Was it helpful?

Solution

No, it's not a singleton (where did you get that idea?). Apart from the fact that it's a static nested class (and that does not imply that it's a singleton), it's a normal class as any other - in particular, you can create as many different instances of NestedClass as you want. If you need it to be a singleton, then you'll have to explicitly code it yourself.

OTHER TIPS

In Java a nested class is an inner class that is declared static.

No. In Java an inner class is a nested class that is not explicitly or implicitly declared static. JLS #8.1.3. You have this back to front.

I am wondering if a nested class is a singleton by default

No.

Nesting, static, and inner have nothing to do with singletons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top