1

Possible Duplicate:
Difference between static class and singleton pattern?

I was wondering,

Would a class such as Java's Math class, where all methods are static be considered a singleton? Or does a singleton have to have an instance, eg: Math.getInstance().abs(...) to qualify as a singleton?

Thanks

Community
  • 1
  • 1
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 2
    Note that, regardless of whether it qualifies as singleton, it's effectively just as global, which makes it subject to most of the criticisms also put forward against singletons, all other things being equal. –  Nov 27 '12 at 16:53
  • See previous thread: http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – jco.owens Nov 27 '12 at 16:56
  • 1
    Did you really mean `static class`, or class with static methods? – Rohit Jain Nov 27 '12 at 16:58
  • `Math.class` can be considered a singleton since you can save a reference to a unique statically reachable instance of an object. You can't save a reference to a `Math` and call methods on that instance. – zapl Nov 27 '12 at 17:10

6 Answers6

9

Having just static methods in a class does not qualify it being a Singleton, as you can still make as many instances of that class, if you have a public constructor in it.

For a class to qualify as Singleton, it should have private constructor, so that it can't be instantiated from outside the class, and have a static factory that returns the same instance everytime invoked.

If you really mean static class, then first of all, you can't have your top-level class as static. You can only have static nested class, in which case you don't need to create any instance of that class, but you can and you can create multiple instances and hence it as not Singleton.

Also, the class you mentioned - java.lang.Math, is not a static class. You should see the documentation of that.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 3
    -1 This isn't about classes in general, it's about static classes. –  Nov 27 '12 at 16:53
  • 2
    @delnan.. Well, I thought OP is talking about class with static methods, as he mentioned `Math` class. And also we can't have a `top-level` class as static. So, may be OP have not framed his title clearly. – Rohit Jain Nov 27 '12 at 16:57
  • @delnan.. I think you can remove your downvote, as I have quoted this thing in my answer. – Rohit Jain Nov 27 '12 at 16:58
  • 1
    It seems pretty clear to me (but then again, I'm not a Java guy). A "static class" need not use the keywords `static class` together in my mind. It's just a class of where there are no instances, regardless of how that's achieved (such as an unused `private` constructor on a `final` class). Edit: The answers here indicate that, among Java experts, the term is taken to refer to whatever `static class` does. If so, my bad, though I still think it's pretty obvious that OP did use the term as I described it. –  Nov 27 '12 at 17:01
  • @delnan: For *every other* concept in Java, you'd associate "static X" as "an X with the static modifier". As such, the term "static class" best fits a class declared as "static class Foo". Yes, the OP was misusing the term - there's simply no term in Java for "a class with no accessible constructors, no instance members, and no way of creating an instance". – Jon Skeet Nov 27 '12 at 17:14
  • @delnan.. Well, `static class` are really written as `static class`, but they are embedded in some top-level class. – Rohit Jain Nov 27 '12 at 17:14
2

Static classes in Java are just nested classes which aren't inner classes. (They're not like static classes in C#, for example.) They can still have instance methods, state etc - and there can be multiple instances.

java.lang.Math is not a static class.

And no, a class which never has an instance is not a singleton. The important difference is that a singleton can implement an interface (or even derive from an abstract class) whereas if you never create an instance of a class, any instance methods are pointless.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

A class that is applied Singleton Pattern has one or none instance at any time on a JVM. That's why it's called single-ton. Having static or non-static members has no relationship with being singleton or non-singleton.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
1

Static classes usually provide some helper methods. In fact i don't think its appropriate to compare Static classes with Singleton. Both are completely different.

We can create multiple instances of a static class but singleton guarantees(atleast in theory) only single instance.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
  • 1
    Static classes in Java are just nested non-inner classes. You can absolutely instantiate them, they can have state etc. – Jon Skeet Nov 27 '12 at 17:00
  • Agree Jon; thats why i have used word "usually" – rai.skumar Nov 27 '12 at 17:02
  • Not in the first part you didn't: you claimed that "static classes just provide some helper methods and doesn't get instantiated"; that's simply not true. – Jon Skeet Nov 27 '12 at 17:14
  • Now your answer has parts contradicting each other. In the first statement you said you can't instantiate static class, and in the Edit, you reversed your claim. – Rohit Jain Nov 27 '12 at 17:19
  • @rai.skumar.. Then update your answer accordingly. There is not point in having an upvoted answer (I don't know how), contradicting itself. – Rohit Jain Nov 27 '12 at 17:22
0

It would have to have an instance with Singleton meaning to only be initialised, once.

Edit: Here's a link to the Wikipedia article

Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

A singleton guarantees that there is only up to one instance of that class within the environment. It is different from a class with a bunch of static methods. A singleton carries state and can carry runtime values, while static methods of a class don't.

You can argue that you can have static values to do that, but the purpose of static variables is different (and sometimes abused like global variables). So a singleton operates in a different context than static methods.

A singleton also is usually associated with some resource (some connection to another server, access to some file, etc.) for which a static instance wouldn't be that ideal. Singletons also allow for lazy loading (if connecting or accessing a resource is expensive), while classes get loaded when the classloader encounters a reference to them.

jbx
  • 21,365
  • 18
  • 90
  • 144