473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What is the use of a static method?

Hi,

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

Thanks,
Doru
Feb 27 '06 #1
5 10275
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
Thanks Kevin
Feb 27 '06 #3
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
2
by: Tony Johansson | last post by:
Hello Experts! I know that you can't have virtual static methods and I know what a static method is. A static method exist only one time no matter how many object you have. You have this...
4
by: DDE | last post by:
Hi All, Reformulating a problem from a previous post: How can I access an application variable from a static method? When I try: string myString = Application{"thisString"].ToString; ...
3
by: bauscharln | last post by:
hoi, I just wanted to add a static method to my interface, but that's not allowed! Is there a reason for this??? (Yes, I could make an abstract base class, but that's not what I want, since I...
6
by: rlvladbob | last post by:
Hi, I've try to access a static method using an instance instead of a class. public class test{ public static void ShowAText(string ThisText) { System.Console.WriteLine("->{0}",ThisText); }
6
by: gg9h0st | last post by:
i really wander what makes static method special? in fact i can access a non static method statically using '::' class aclass { function anonstatic() { echo 'non static'; } static...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
13
by: =?Utf-8?B?bWFyaw==?= | last post by:
Consider this a continuation of 2/27/2007 thread by Zytan entitled"Subject: Does C# have static local variables like C++?" In C++: void mysub() { static double x; blah; blah; }
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
5
by: DamienS | last post by:
Hi, I have a static method in a class and I need to be able to return a reference to "this". Googling around, I found a heap of discussions of the pros/cons of "abstract static" etc. It was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.