This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Singleton Objects

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I know about the singleton objects and how we can achieve this.
My question is If I make all the members and methods of my class as static then there will be only one copy of them crearted.
Please help me to understand when I should go for Singleton Object? and what are the advantages/disadvantages over the class as I describe above?
Thanks,
Sachin
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q) I know about the singleton objects and how we can achieve this.
A)
One of the way to achieve your objective is
1. Declare the constructor as private static final.
2. Declare one method as static with return current class (example getInstance as shown.)
3. create required methods as per the requirement.
public SingletonTest{
private static final SingletonTest SINGLETON = new SingletonTest();


priavate SingletonTest()
{}
public static SingletonTest getInstance()
{
return SINGLETON;
}

public TestPrint(String strName)
{
System.out.println("Name passed is "+ strName);
}

}

Q)what are the advantages/disadvantages over the class as I describe above?
Many times you might require only one instance of class is made used by a application during its execution.
Example one part of the application set some value to the class variable and some other point of time another part of application want the value same value set. Since same instance is serviced for all request thought out the application execution. This can be easily achieved by following Singleton Object.

Using singleton model when you really don�t require it and ending up in a mess is the disadvantage part which will not occur if it is understood and used.
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, singleton instances can implement interfaces, be extended, etc - something that is not easy with classes containing static methods.
Moving this to the Patterns forum...
Simon
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also beware that the Singleton pattern (and the degenerate case of using static methods) have a lot of drawbacks. Do a search in this forum for "singleton" and you will finda whole lot of discussion about all these points.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!