Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When i want to use a static method?

51 views
Skip to first unread message

Fernando Lopes

unread,
Apr 22, 2005, 1:23:53 PM4/22/05
to
Hi there!
Someone has some code sample about when is recommend use a statis method?
I know this methos don't want to be initialized and all but I want to know
when I need to use it.

Tks.
Fernando


DKode

unread,
Apr 22, 2005, 1:32:25 PM4/22/05
to
I use static methods when I need to get something that is relevant to
that class but I don't want to create an instance everytime to get this
information. With a static method you cannot access
methods/properties/variables from the class. The only variables
available are within the static method you are calling.

W.G. Ryan eMVP

unread,
Apr 22, 2005, 1:41:39 PM4/22/05
to
Typically Helper and Utility methods are good candidates. Often times as
well, you may want to create Static as well as instance objects. Looking to
the framewokr, there's the File and the FileInfo, the Directory and
directoryInfo. Similarly, check out the Data Access Application Block - all
of the SqlHelper methods are static - and as a rule of thumb, anything you'd
name xxxHelper usually is a great candidate for being implemented as a
static.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Fernando Lopes" <fernandomattarlopes@[remove]msn.com> wrote in message
news:ueNMMA2R...@TK2MSFTNGP10.phx.gbl...

Bruce Wood

unread,
Apr 22, 2005, 2:19:03 PM4/22/05
to
I think that you should also make a distinction between when to create
_public_ static methods, and when to create _private_ static methods.

All of the information given here so far is valid for public static
methods. If your class is exporting a static method, it's usually
because it's functionality that belongs with your class, but for which
you don't want your callers to have to create an object just to use it.
For example, most Parse methods are static, because you don't want your
caller to have to create a throw-away object just to parse a string to
make the object they really want. To illustrate:

MyClass parsedObject = MyClass.Parse(inputString);

is better than

MyClass parsedObject = new MyClass().Parse(inputString);

because the Parse method doesn't need anything from a particular
MyClass in order to do its job (it just needs the string), and why
should your caller create a throw-away MyClass (new MyClass()) in order
to use the method?

_Private_ static methods come with different considerations.

Whenever you make an instance private method, you're giving that method
access to all of the fields in your class. Sometimes you don't need
that. Sometimes you just need a little "helper" method that does a
small job. Rather than give it access to your class's state, you can
make it static and pass it everything it needs in order to do its job.

This is easier to read and easier to maintain. A static private method
says, "You don't need to worry that somewhere inside my code I use some
fields of this class... or, even worse, that somewhere buried in my
code I _change_ some fields in this class. The only information I need
is in my parameter list. The only information I modify is in my
parameter list and in my return value."

Not only that, but the static private method can't call any instance
methods, so it can't modify an object in any other way.

In other words, static private methods come with many fewer assumptions
that private instance methods. For that reason they're attractive.
However, if you have to pass a dozen fields to a private static method,
then it should probably be an instance method. There's a balance to be
struck: if an instance method needs only one or two instance fields,
consider making it static. If a static method needs a dozen instance
fields passed to it, consider making it an instance method.

CheshireCat

unread,
Apr 22, 2005, 2:39:03 PM4/22/05
to
"DKode" <dko...@gmail.com> wrote in message
news:1114191145.3...@z14g2000cwz.googlegroups.com...
Not quite right, you can also access static class variables (and consts) or
static properties


KH

unread,
Apr 22, 2005, 5:04:01 PM4/22/05
to
> I know this methos don't want to be initialized

All methods are static in a sense - even instance methods. The logic of
instance methods does not get copied every time an instance of its class is
created, it lives in one place and a pointer to the instance is passed to
that method - that's what the "this" pointer is, it's just done for you.

Basically what happens is that the compiler sticks a pointer to an instance
of the class as the first argument. So for class Foo, you define a method:

int Bar(int i) { return(this.x + i); }

and the compiler turns it into this:

int Bar(Foo f, int i) { return(f.x + i); }

If a copy of each method was made for each object allocation there would be
huge and unnecessary overhead; think of a class like String with a ton of
methods being copied all over the place all the time.

Landi

unread,
Apr 22, 2005, 5:43:10 PM4/22/05
to
Good info Bruce.
Also, thanks for taking the time to write that long explanation.

--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

"Bruce Wood" <bruc...@canada.com> wrote in message
news:1114193943.8...@l41g2000cwc.googlegroups.com...

Jon Skeet [C# MVP]

unread,
Apr 27, 2005, 1:26:57 PM4/27/05
to
Bruce Wood <bruc...@canada.com> wrote:
> This is easier to read and easier to maintain. A static private method
> says, "You don't need to worry that somewhere inside my code I use some
> fields of this class... or, even worse, that somewhere buried in my
> code I _change_ some fields in this class. The only information I need
> is in my parameter list. The only information I modify is in my
> parameter list and in my return value."

Unless there are static fields in the class of course - and a static
method can call private methods (or modifiy private fields) on any
instances of the class it's able to get hold of (whether parameters,
static fields or whatever).

Often it's nice to have a static method which takes two instances of
the class just because it's not specifically applicable to one instance
"more" than the other instance.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Bruce Wood

unread,
Apr 27, 2005, 3:31:37 PM4/27/05
to
> Unless there are static fields in the class of course - and a static
method can call private methods (or modifiy private fields) on any
instances of the class it's able to get hold of (whether parameters,
static fields or whatever).

Agreed. Nonetheless, I think it's easier to maintain a private method
that takes one or two extra arguments and is static than a private
instance method that uses only one or two instance fields, because you
don't really know what the latter is doing without examining the code.

0 new messages