Re: static or not?
- From: iamfractal@xxxxxxxxxxx
- Date: 20 May 2005 00:38:28 -0700
David Vanderschel wrote:
> There are many situations when one has a Class for
> which it only makes sense that a single instance be
> created in a given session. It seems to me that, in
> this situation, it does not really make much
> difference whether the Class's variables are declared
> as static or not. There is still only going to be one
> instance of each anyway. I have determined by
> experiment that such a program compiles and works the
> same either way. (I have encountered a couple
> situations where the compiler was not happy until I
> made a certain variable static because I had
> referenced it from a static context. But such
> situations are rare for me.)
>
> My question: When you have the choice, which choice
> should you make? My guess is that static declarations
> may lead to more efficient code; but I have been
> unable to find any discussion of this issue. (Part of
> the problem here is that Sun's search on "static" hits
> just about everything. Its concept of relevance seems
> pretty weak.) If it makes no difference, then I see
> no point in cluttering the code with unnecessary
> "static" key words.
>
> I could use some guidance on this issue.
>
> Any pointers appreciated,
> David V.
Hi, David,
When I need a single instance, I use a singleton (with a non-private
constructor, admittedly), with a static getInstance() method and all
other methods and data non-static.
I have no idea whether static data/methods lead to more efficient code,
but I think if your application depends upon such efficiency gains then
perhaps a non-interpreted language is better suited to your needs.
The minor reason I use as many non-static data/methods as possible is
because it makes it slightly easier to subclass the class if I find
that I was wrong in my assumption of the class's singularness, and
that perhaps two specific variants of the class are more suitable. If
it were a significant effort to use a singleton over using all static
data/methods, then I wouldn't bother; but singletons are, I find, easy
to design and use.
The major reason I use a singleton applies only when the singleton is
declared as public, i.e., is accessible outside its package.
Dependencies on concrete classes outside their home packages are more
dangerous than dependencies on concrete classes within their home
packages, as packages should be as encapsulated at possible/convenient.
So I prefer to cite the first principle of OO ("Program to an interface
not an implementation") when dealing with public classes, and try to
access the services they offer via an interface (or an abstract class)
rather than by the concrete class itself. Thus, I can alter the
implementation of the concrete class without affecting its clients.
I generally do this by having the singleton register itself in a public
Registry class. Yes, this is a concrete class visible to all packages,
but I keep this class as simple as possible: it only stores and returns
interface, and particluarly has no dependencies on the methods
contained in those interfaces, so I can change the the entire set of
methods within any given interface without changing the Registry code.
If I use a singleton, then I can strip away an interface from this
singleton, and have it register that interface in the Registry.
If I use static methods, I cannot do this: static methods are not
allowed to hide the instance methods of the interface.
This is the major reason I use a singleton, instead of a class of
static methods.
..ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.
.
- Follow-Ups:
- Re: static or not?
- From: hawat.thufir@xxxxxxxxx
- Re: static or not?
- References:
- static or not?
- From: David Vanderschel
- static or not?
- Prev by Date: Re: path to ant; ant launcher
- Next by Date: Re: problems with Jakarta Commons HttpClient
- Previous by thread: static or not?
- Next by thread: Re: static or not?
- Index(es):
Relevant Pages
|
|