Connecting Tech Pros Worldwide Help | Site Map

When to use 'static' for a method?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2005, 05:37 PM
Dave
Guest
 
Posts: n/a
Default When to use 'static' for a method?

Hi,
Is there a general rule to use 'static' on a class member? It seems
uneccessary to have to create an instance of an object just to use it's
methods where declaring something as static makes more sense.

Thanks

  #2  
Old November 16th, 2005, 05:37 PM
Peter Rilling
Guest
 
Posts: n/a
Default Re: When to use 'static' for a method?

It depends on the problem you are trying to solve, like all computer related
constructs. There is no rule, just whether or not it makes sense for your
particular problem.


"Dave" <Dave@discussions.microsoft.com> wrote in message
news:71BA16AC-0451-4D5E-9715-E873ABB59CA6@microsoft.com...[color=blue]
> Hi,
> Is there a general rule to use 'static' on a class member? It seems
> uneccessary to have to create an instance of an object just to use it's
> methods where declaring something as static makes more sense.
>
> Thanks[/color]


  #3  
Old November 16th, 2005, 05:37 PM
Anders Norås [MCAD]
Guest
 
Posts: n/a
Default Re: When to use 'static' for a method?

Dave wrote:[color=blue]
> Hi,
> Is there a general rule to use 'static' on a class member? It seems
> uneccessary to have to create an instance of an object just to use it's
> methods where declaring something as static makes more sense.[/color]
Instance methods are instance methods because they rely on the state of
the specific object instance. Instance methods are tied to a particular
instance because the behavior that the method invokes relies upon the
state of that particular instance.

When you declare a method as static, you define that method as being a
class method. A class method applies to the class as opposed to any
particular instance. The behavior instigated by a class method does not
rely on the state of a particular instance. In fact, a static method
cannot rely on an instance's state since static methods lack access to
this reference. Instead, the behavior of a class method either depends
on a state that all objects share at the class level, or is independent
of any state at all.

If a method relies on an object instance's state it should be an
instance methods. If a method is general for all or no instances of a
class, and does not rely on the object state, it should be a static method.
Instance methods are most commonly used. However static methods are very
useful for utility and factory classes amogst many other uses.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
  #4  
Old November 16th, 2005, 05:37 PM
Bruce Wood
Guest
 
Posts: n/a
Default Re: When to use 'static' for a method?

Your question almost reads like the correct answer: if a method _could_
be static (in other words you make it static and compiler doesn't whine
that it can't find some instance variables any more), then it probably
should be. There are reasons why a method needs to be an instance
method even though it doesn't use any instance members, but it's much
more common that a method that doesn't access object state should be
static, because, as you pointed out, it's unnecessary to create an
object instance for no other reason than to call a method and then
throw away the object.

So what are the exceptions? They have to do with polymorphism. Look at
the Strategy pattern in the Design Patterns book: this is an example in
which your object may have no state (no member variables) at all, but
all of your methods may be instance (non-static) methods, because you
want to take advantage of inheritance and polymorphism.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.