473,385 Members | 1,922 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,385 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 22350
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.