?
Solved

Singleton Vs Static

Posted on 2005-04-27
10
Medium Priority
?
255 Views
Last Modified: 2008-03-06
Hello,

          I often come across this question, when will you decide you want a singleton class, when will you make your variables/methods static  ??

Thank You,
0
Comment
Question by:letsbedecent
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 1400 total points
ID: 13879166
I think a Singleton is a better approach then having a utility class with only static methods.
One reason is that a Singleton can implement an interface and allow the Singleton Factory to return different singleton based on a given type.
That can happen per request or allow will allow you to change the implementation of the singleton without changing or compiling the classes who uses it.
0
 
LVL 3

Expert Comment

by:neonlines
ID: 13879190
The link explains completely about singleton and how to use ur static variables

http://www.javacoffeebreak.com/articles/designpatterns/
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13879199
This link has some more arguments that favour the Singleton aproach: http://www.odi.ch/prog/design/singleton.php
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:neonlines
neonlines earned 400 total points
ID: 13879221
0
 

Author Comment

by:letsbedecent
ID: 13879318
>>That can happen per request or allow will allow you to change the implementation of the singleton without changing or compiling the classes who uses it.

If we use a utility class that has static methods, and change the methods implementation, we still need not change the classes that call this method right ??
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13879357
>> If we use a utility class that has static methods, and change the methods implementation, we still need not change the classes that call this method right ??
Right, but if you want to replace it with a different class that provides similar functionality.
0
 

Author Comment

by:letsbedecent
ID: 13879386
Is that a common need ??

is this the reason a singleton was conceived ??
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13879420
>> Is that a common need ??
It just make your application more flexible to changes (always a good thing to desing/code this way)

>> is this the reason a singleton was conceived ??
No, the main reason is to have only once instance per application.
There are several ways to achive it though Singleton is a more modular and flexible achive it.
BTW, to be precise it is not really a singleton per application but rather a singleton per classloader.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 13882104
I think it's useful when we need to get a reference of main frame at anywhere of code without passing the reference into the calling class.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 13883680
I generally use Singleton classes for manager-classes like some database-manager, state-manager, etc and utility classes with static methods for simple utilities like String manipulations, date-time conversions, etc.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question