aspose file tools*
The moose likes Java in General and the fly likes What is diffrence between Singleton and a Nested class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is diffrence between Singleton and a Nested class" Watch "What is diffrence between Singleton and a Nested class" New topic
Author

What is diffrence between Singleton and a Nested class

Ashish Kothari
Ranch Hand

Joined: May 16, 2003
Posts: 37
What is diffrence between Singleton and a Nested class?
I think nested class is same like singleton except that is inside a class.
please help.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
hi:
please refer to:
Introduction to Inner Classes;
Java vs. Delphi;
Code Style Guide.
peter greaves
Ranch Hand

Joined: Sep 27, 2002
Posts: 51
singleton is best understood as a pattern that solves a design problem : how to ensure that only one instance of a class exists. the usual strategy to make this happen is:
1. stop clients making instances of the class by declaring a private constructor.
2. make the class contain a single instance of itself as a member.
3. provide a static method for returning that single instance. very crudely:

public class MySingleton{
private MySingleton(){}
private MySingleton singleton = null;
public static MySingleton getInstance(){
if (singleton == null){
singleton = new MySingleton();
}
return singleton;
}
}
now, inner classes are (generally) a useful method of encapsulating class-only functionality inside fully fledged objects - they solve the problem of unwieldy implementations and excessively sprawling class heirarchies. so they solve an implementation problem , not a design problem. maybe you have some particular issue that lead to the question that you could share?
peter


SJCP 1.2
Ashish Kothari
Ranch Hand

Joined: May 16, 2003
Posts: 37
Thanks,peter,though,I know the exact details of both but I was unable to explain it somebody who asked .
One more question I would like (that is diffrent from this one),I want to swap two strings thru a method like swap(String str,String str1).
but passing thru reference does not work,do you know How can I swap values of String thru method.
Is this possible as strings are immutable.
 
 
subject: What is diffrence between Singleton and a Nested class