This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes Static  methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static  methods" Watch "Static  methods" New topic
Author

Static methods

Suneesh Raman
Ranch Hand

Joined: Jun 13, 2002
Posts: 42
I having the following questions

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

Thanks,
Suneesh VR
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Static methods are intended to do operations which are not dependent on concrete instances of a class, but are for general purposes.
java.lang.Math is an example.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


what circumstances we go for static method .


One easy thumb rule -
if your method doesnt operate on any instance variables or call other non-static methods then it (your method) ought to be static.

ram.
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
Imagine one typical scenario.

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.


"There are no mistakes, only lessons"
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
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 ]

Thnx
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Uh oh, looks like my turn to try this and see what happens.
 
Don't get me started about those stupid light bulbs.
 
subject: Static methods