473,287 Members | 3,319 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,287 software developers and data experts.

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 22340
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
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
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: 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
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
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
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
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
by: Doru Roman | last post by:
Hi, Can somebody explain please the meaning and use of a STATIC method? Thanks, Doru
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.