Solved

Difference why Singelton Class and Static Class

Posted on 2009-04-07
4
486 Views
Last Modified: 2013-11-23
Can anyone tell me the difference between a singleton class and a Static class, as with both mechanism we can create only single instance per of a class.
I would appreciate if someone can explain me the difference with example.

0
Comment
Question by:sunsim24
4 Comments
 
LVL 12

Expert Comment

by:basav_com
ID: 24091856
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 24092698
theres really no such thing as a singleton class. A psuedo singleton is mostly implemented using static.

0
 
LVL 4

Expert Comment

by:aman123_123
ID: 24093935
There is nothing as a Static class in Java. You should make your a class singleton when you only want a single instance of the class in your application.
Also , you should make a class singleton when you do not want to maintain the state of its instances. For example, every object of the following class will have a different state.

class Point {
int x,
int y
}

However, if you have a class whose state is not be maintained, then you should make it singleton. For example

class PointUtil {
public Point isAbove(Point a, Point b) (...);
public Point isOnRight(Point a, Point b) (...);
}

Such classes you should make singleton, as two instances of this function will be of no use.

You can also achieve the same by having all functions as static but static functions are loaded at startup , so you are better off making it a singleton.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse compiler vs Installed JREs option 3 75
Java array passed to SQL where clause 23 62
eclipse java  build path 6 43
java jdbc batch example 8 19
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now