473,654 Members | 3,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10286
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*******@roge rs.com> wrote in message
news:u5******** ******@TK2MSFTN GP09.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*******@roge rs.com> wrote in message
news:u5******** ******@TK2MSFTN GP09.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*******@cana da.com> wrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.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
15361
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 the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly...
2
3156
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 static method even if you have no objects at all. Static methods belongs only to the class and not to the object you have. They can only access static members.
4
7557
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; whithi a static method, I get the message: CS01020 "In order to use a non-static field, method, or property, you
3
2785
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 don't need an implementation.) thank you in advance, bauscharln
6
5766
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
31738
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 function astatic() {
5
8858
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 created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
13
1470
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
5839
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
5
1828
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 quite a heated debate about purity of OO design that just did my head in a bit. In a nutshell. Can a static method 'know' what class it's defined in and return that type information?
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.