424,303 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

What is the use of a static method?

P: n/a
Hi,

Can somebody explain please the meaning and use of a STATIC method?

Thanks,
Doru
Feb 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
A static method is a single instance of a method that exists in the heap and
is used by all client code. A non-static method is a method that is created
when an instance of a class is created, and is specific to that class. For
every instance of the class, there will be an instance of the method.

Static methods are used for operations that do not require the use of
instance members of a class. The System.Math methods are excellent examples
of this. They take parameters, perform mathematical operations on the
parameters without requireing any instance members of the Math class to do
so, and return a value to the caller without modifying the Math class in any
way.

They are useful because they conserve memory and processor resources by
virtue of there being only one copy of the method. They can be a bit tricky
though, because every instance of every class is using the same instance of
the method, and this can cause thread-related conflicts.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"Doru Roman" <do*******@rogers.com> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
Hi,

Can somebody explain please the meaning and use of a STATIC method?

Thanks,
Doru

Feb 27 '06 #2

P: n/a
Thanks Kevin
Feb 27 '06 #3

P: n/a
KH
To the best of my knowledge, instance methods are not created each time an
instance is created and do not conserve memory over static ones ... in a
sense all methods are static because the instance of the class is passed to
instance methods as a hidden parameter and is accessible via the <this>
keyword, example:

class T
{
int member;

int add(int value) { return this.member + value; }
};

The method add(int) becomes, essentially:

int add(T instance, int value) { return instance.member + value; }

"Kevin Spencer" wrote:
A static method is a single instance of a method that exists in the heap and
is used by all client code. A non-static method is a method that is created
when an instance of a class is created, and is specific to that class. For
every instance of the class, there will be an instance of the method.

Static methods are used for operations that do not require the use of
instance members of a class. The System.Math methods are excellent examples
of this. They take parameters, perform mathematical operations on the
parameters without requireing any instance members of the Math class to do
so, and return a value to the caller without modifying the Math class in any
way.

They are useful because they conserve memory and processor resources by
virtue of there being only one copy of the method. They can be a bit tricky
though, because every instance of every class is using the same instance of
the method, and this can cause thread-related conflicts.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"Doru Roman" <do*******@rogers.com> wrote in message
news:u5**************@TK2MSFTNGP09.phx.gbl...
Hi,

Can somebody explain please the meaning and use of a STATIC method?

Thanks,
Doru


Feb 27 '06 #4

P: n/a
> To the best of my knowledge, instance methods are not created each time an instance is created and do not conserve memory over static ones.

Correct.

All that "static" means is that the method does not alter the instance
fields of the class: the instance's "state", if you will. So, if you
declare an instance field like this:

private int myCount = 0;

then declaring a method "static" means that it doesn't need to use or
modify myCount (for example). Publi static methods are useful in two
situations: 1) when the method is just a "helper method" that does some
work but doesn't read or change any instance fields; 2) when the method
in question is there in order to create a member of the class, and you
don't want your callers to have to create a "fake" instance of the
class in order to call an instance method in order to create the
instance that they really want.

See also this thread on when to use static methods:

http://groups.google.com/group/micro...36ac0511f11f19

Feb 27 '06 #5

P: n/a
Yes, that's right, sorry. I keep forgetting that .Net works this way.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
To the best of my knowledge, instance methods are not created each time
an instance is created and do not conserve memory over static ones.


Correct.

All that "static" means is that the method does not alter the instance
fields of the class: the instance's "state", if you will. So, if you
declare an instance field like this:

private int myCount = 0;

then declaring a method "static" means that it doesn't need to use or
modify myCount (for example). Publi static methods are useful in two
situations: 1) when the method is just a "helper method" that does some
work but doesn't read or change any instance fields; 2) when the method
in question is there in order to create a member of the class, and you
don't want your callers to have to create a "fake" instance of the
class in order to call an instance method in order to create the
instance that they really want.

See also this thread on when to use static methods:

http://groups.google.com/group/micro...36ac0511f11f19

Feb 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.