Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods

 
Suneesh Raman
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh oh, looks like my turn to try this and see what happens.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic