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
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: What is diffrence between Singleton and a Nested class