• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is diffrence between Singleton and a Nested class

 
Ashish Kothari
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
please refer to:
Introduction to Inner Classes;
Java vs. Delphi;
Code Style Guide.
 
peter greaves
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Kothari
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic