Java static methods versus Objects

R

Robert

Hi,

Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about
the case of classes implementing Runnable interface. Just as theory. Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.

Thanks in advance
Robert
 
T

Thomas Weidenfeller

Robert said:
Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class?

You deprive yourself of being able to use all those nice methods which
only work on instances, not classes.

You might think now that you don't need any of them, but more often
than not you find out later that it would be nice to be able to use
them.
Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.

This boils down to the question of why using an object-oriented
language at all. In fact, if you don't like or want objects, than
another language might indeed be a better choice.

/Thomas
 
M

Michael Borgwardt

Robert said:
Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about
the case of classes implementing Runnable interface. Just as theory. Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.

Because with static methods you cannot later switch to multiple instances without
changing the client code, and you can't extend the class and override methods.
 
V

VisionSet

Robert said:
Hi,

Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about
the case of classes implementing Runnable interface. Just as theory. Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.

If you need to inherit from the class, a singleton will work properly.
Static methods/attributes mean you only get one of them, no matter how many
subclasses there are.

What happens when you decide later you want more than one? It is very
simple to change a Singleton to accomodate.
 
R

Robert

Do not misuderstend me please. I think objects are usefull when programmer
will use more than one instances. very useful. But if I will use only one
instance of class, in fact I will not use objects. For me object is
nessesery when I need more of them, but at the time when I just create one
instance of class it seems that there is just oone more piece of code
added(object initialization) but in fact no functionality.

But you are right that there is some functionality which can be used only on
object. That is why I wrote that passage of threads for example.

Anyway thank you for answer
Robert
 
M

Michael Borgwardt

Robert said:
Do not misuderstend me please. I think objects are usefull when programmer
will use more than one instances. very useful. But if I will use only one
instance of class,

You *never* know what you "will" use. Specifications change all the time,
and your code needs to be able to follow.
 
D

Dave Monroe

Robert said:
Hi,

Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about
the case of classes implementing Runnable interface. Just as theory. Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.

Thanks in advance
Robert

It's a performance issue.

Static methods don't guarantee freedom from load time penalties.

For infrequently used classes, static methods are okay (e.g.
java.lang.Math).

If you have a lynchpin type class (that basically holds your
application together), you want it to be in memory. But, since many
other threads will be using it, you don't want everybody to have a
unique instance, otherwise you get into garbage collection issues.

As an added bonus, if you write good thread-safe code for your
singleton object, the JVM handles the threading for you.

Dave Monroe
 
C

Chris Smith

Robert said:
Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about
the case of classes implementing Runnable interface. Just as theory. Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.

There are a few cases where you might want to use an instance of a class
as opposed to static methods and fields. These include:

1. You want to take advantage of inheritance and/or polymorphism. Even
if there's only one instance, it may still implement interfaces declared
elsewhere and extend classes declared elsewhere. These superclasses and
superinterfaces could allow the class to be used in ways that it
couldn't with only static methods.

2. You want to be able to manipulate data in a way intrinsic to an
object. Again, having only one object of a specific class doesn't mean
that, for example, you don't want to add that object into a list
somewhere or otherwise treat it as an independent piece of data from the
perspective of another part of the application.

3. You anticipate future expansion making the object less unique. For a
large group of problem-domain objects, shifting from one unique instance
to managing multiple instances is actually a fairly common way for the
scope of the application to expand in future versions.

There are probably more reasons as well, but those are the reasons that
come to mind.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Dave said:
It's a performance issue.

Static methods don't guarantee freedom from load time penalties.

For infrequently used classes, static methods are okay (e.g.
java.lang.Math).

If you have a lynchpin type class (that basically holds your
application together), you want it to be in memory. But, since many
other threads will be using it, you don't want everybody to have a
unique instance, otherwise you get into garbage collection issues.

As an added bonus, if you write good thread-safe code for your
singleton object, the JVM handles the threading for you.

Dave, your post contains some very interesting ideas that I haven't
heard of before. Do you have a reference where these things are
explained? Some of them don't make any sense to me, given what I know
about the VM and how it operates.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

Peter J. Grey

Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about

In general, a singleton object is probably a little more "natural" than a
degenerate (static member only) class, but really you have to make a judgement
call. Global objects (and they do exist) never seem to be entirely comfortable
in the object-oriented scheme of things. Generally, if there's any kind of
internal state, I would go with an object, but if it's more intuitive as a
pseudo-global function, then static members would probably make more sense.

Or do both! I have a general-purpose class "Sys" implementing various
system-wide facilities, basically a facade pattern for bits and pieces of
System, Runtime, ClassLoader, etc. It has only static members, but it creates a
temporary object, with all the benefits of polymorphism, during its
initialization.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top