473,503 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 16 '05 #1
3 22366
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" <Da**@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
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

Nov 16 '05 #2
Dave wrote:
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.

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/
Nov 16 '05 #3
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.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
15569
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self):...
5
15336
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
2057
by: Samba | last post by:
Hi, Mine is a very basic doubt ... (1) If we are using a static field/object, then the field/object is shared across all the places in the entire application as it is stored only at one...
4
7547
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; ...
1
1721
by: Jose | last post by:
Hello: I'm preparing a multitier application, and in the Data Layer I have a EmployeeRepositoryBase class that has a protected static method called Fill() and some GetAll() overloaded protected...
8
2045
by: Fernando Lopes | last post by:
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....
6
5762
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); }
5
10280
by: Doru Roman | last post by:
Hi, Can somebody explain please the meaning and use of a STATIC method? Thanks, Doru
13
1462
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
5830
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...
0
7199
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
7076
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
7274
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,...
0
7323
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...
1
6984
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...
1
5005
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...
0
4670
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...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
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...

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.