File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes confuse about singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "confuse about singleton" Watch "confuse about singleton" New topic

confuse about singleton

Jeru Liu

Joined: Aug 14, 2001
Posts: 23
Why java.lang.Math class is a singleton class,why it need to be a singleton,
and the SingletonSet in java.util.collections is a Singleton exmaple too,why it should be a singleton?
[This message has been edited by Jeru Liu (edited August 15, 2001).]
Tahir Mansoori

Joined: Jul 25, 2001
Posts: 29
Math class is not a singelton. Singleton is a Creational Pattern (GoF) and it is used to instantiate only single instances of a class. Suppose yr Database supports only single connection and your classess want to use only that one instance of connection. then you can use it like.
class ConnectionProvider
static Connection cn=null;

Connection getConnection()
return cn;
return (cn=new Connection());

Hope this helps
Tahir Mansoori
Jeru Liu

Joined: Aug 14, 2001
Posts: 23
thank for you reply very much
I do know what is singleton mean and how to implement it in serveral method,but I am pretty sure that Math is a example of
Static Classes as Singleton Patterns
There already is a kind of Singleton class in the standard Java class libraries:
the Math class. This is a class that is declared final and all methods are
declared static, meaning that the class cannot be extended. The purpose of
the Math class is to wrap a number of common mathematical functions such
as sin and log in a class-like structure, since the Java language does not
support functions that are not methods in a class.
These words come from Design Pattern for Java Companion
and i had heard that Math is singleton example from other resouce as well.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
java.lang.Math is definately NOT an example of a Singleton. Math isn't really an object at all. It is in the sense that it is declared as a class, but it is never instantiated ANYWHERE. Not even withing the Math class itself. You cannot say 'Math m = Math.instance()' (which you might do if Math were a singleton).
Math is a helper class of sorts. It has assorted functions that you can pick and choose as you need them -- this is not an object oriented design pattern (it is much closer to a functional design). Math does not hold data or maintain state. Since it doesn't store anything, it doesn't need to be an object (and therefore shouldn't be a Singleton -- which is isn't).
If I were you, I would take whatever books are telling you the Math is a Singleton and get rid of them. If you claim that Math is a Singleton on any Java certification exams, you'll only be hurting yourself.
Paul Gilette

Joined: May 09, 2001
Posts: 10
"Singleton" is a concept. The patterns book is clear that there may be many implementations of the various patterns. So even though Math is not a singleton (for reason's I'll give below), an abstract class CAN be used to implement singleton.
The purpose of a singleton is to have a single copy of data which the class methods access and update. Therefore, besides the classic "private static field with static accessor" technique, you can also just use an abstract class where its static fields are accessed and updated by its static routines. The key is that there is the one copy of data associated with the "object".
Math is not a singleton because it does not represent "a single copy of data" on which the routines act. Instead, it's just a set of utility routines to which you pass the data to be acted on.
Jeru Liu

Joined: Aug 14, 2001
Posts: 23
Thanks for all very much,I think i get the idea now.
I agree. Here's the link:
subject: confuse about singleton
Similar Threads
Synchronization for Singleton
reading files & caching
instance varibales for Singleton class ?
Singletion - memory issue ?