-2

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;
}
Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87
  • 1
    Do you know what a singletone is? – Maroun Nov 26 '13 at 22:18
  • 1
    Your terminology about "nested classes" etc. is incorrect — see http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html: "Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes." – Reinstate Monica -- notmaynard Nov 26 '13 at 22:28

2 Answers2

2

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • It's a static nested class. It isn't an inner class. Inner and static are mutually exclusive. – user207421 Nov 26 '13 at 22:33
  • You are funny asking where did I get the idea from: that's the whole point of my question: I am wondering if one implies the other. Thanks for clarifying. – Cote Mounyo Nov 26 '13 at 22:35
  • @CoteMounyo Everyone else is wondering the same thing. It's a perfectly good question, and you haven't answered it. Where *did* you get the idea? It doesn't make any sense. – user207421 Nov 26 '13 at 22:36
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483