1. What is the significance of static method apart from it can be accessed by Class name with out instance of class?And at what circumstances we go for static method . 2. What is the difference between Sigleton pattern and a class having all static variables and methods
Your application(client-server) needs user id in many modules.So instead of writing System.getProperty("user.name") evverywhere you can write one static method which will return you user id.and access this method from anywhere in the application.This method is not dependant on any other object.
Originally posted by Suneesh Raman: 2. What is the difference between Sigleton pattern and a class having all static variables and methods
A class with all static variables and methods will likely never be instantiated. A singleton is a class that is designed to be instantiated exactly one time so that there is a single instance of that class for the life of the application.
Using a singleton rather than static methods allows you to use inheritence. Perhaps you define an abstract class as the base for the singleton and then extend it to create a many concrete classes that you actually instantiate. At runtime you use some property to choose which concrete class to use for that run.
How can we create instance of a class extending an abstract Singleton class ?.. will this not give compiler error because the singleton will be having private constructor? [ April 25, 2005: Message edited by: Nischal Tanna ]
If you extend a class you can expand the visiblity of any method, so your derived class could have a public constructor or another private one. Try it and let us know what you find.
One problem with static methods and with (most) singleton implementations is that they cause hard references from one class to another. For example, if my application has this line in thousands of places:
Logger.log( message );
it is very hard to replace the Logger class with another one. Many times you get a singleton instance from a static method on the same class:
Logger.getLogger().log( message );
That is some improvement because if I wanted to use a different logger class I could change the getLogger() method to return a new type that extends Logger or implements the same interfaces.
These issues kick in with large systems that will be maintained over time. For small programs and exercises it's not critical to solve them. But knowing how can get you out of trouble some times! [ April 25, 2005: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Nischal Tanna: How can we create instance of a class extending an abstract Singleton class ?.. will this not give compiler error because the singleton will be having private constructor?
You are right, you would need to make the constructor of the Singleton protected, at least. No problem with that, though - design patterns *are meant* to be adapted to the current situation.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus