473,667 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When is it appropriate to use static methods?

Tom
This is not intuitivelly clear.
Nov 16 '05 #1
17 51441
Tom,

Static methods are methods that do not share any variables. the are the
same as VB6 sub routines that our outside of a class.

Example:
class Foo
{
private string thisTest = "test";
public string MyString()
{
return thisTest;
}

public static MyStaticString( )
{
return "test";
}
}

usage:

when using a static method you can just call
string test = Foo.MyStaticStr ing();

if it is not static you have to create and instance of the class

Foo myFoo = new Foo();
string test = myFoo.MyString( );

Basically is all boils down to memory usage. a static method is shared,
where as a non-static method gets created for every instance of the class.
Hope this makes sense?
"Tom" <nf*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
This is not intuitivelly clear.

Nov 16 '05 #2
Use a static method when the method does not belong to a specific object.

For example, if you look at the Math class in .NET framework, you will see
that all methods are static. Why? Because there is no reason to must create
an object to use the methods. Why would you want to create an object of the
Math class, when all you want is the absolute value of something? No, there
is no reason to do this, and therefore, the method is static.

So when you design a class, ask yourself:

Does this method belong to an object, or the class itself?

A method belongs to an object, if it modifies the state of the object. If
the method does not modify a specific object, it can most likely be static.

Another example, suppose that you want to know how many objects of a class
that is created (don't ask me why...). For this task, you could create a
static method GetNumberOfObje cts() (and you obviously need a static field,
and some code in the constructor too). Why would i have it static, you might
ask. Well, answer the above question, and you will see. The method does not
belong to any specific object. Additionally, it does not modify any object.

I hope this makes sense.

--
Regards,
Kristofer Gafvert - IIS MVP
http://www.ilopia.com - When you need help!
"Tom" <nf*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
This is not intuitivelly clear.

Nov 16 '05 #3
Tom

"Kristofer Gafvert" <kg******@NEWSi lopia.com> wrote in message
news:u%******** ********@TK2MSF TNGP09.phx.gbl. ..
Use a static method when the method does not belong to a specific object.


What if the class is a singleton?

For instance, I have a WinForm application that does not allow multiple
instances of the application to run on the same workstation. This
application uses a Business Object singleton that caches global variables,
such as a reference to a DataSet that can be used by multiple MDI child
forms.

It doesn't seem particularly useful or efficient to create a new instance
every time I want to use one of the methods in this singleton.
Nov 16 '05 #4
I would say that the same "rules" apply, no matter if it is a singleton or
not.

But, remember that a static method is not a substitute for not having a
proper class design. Is the method(s) we are talking about really relevant
for the class? Does they belong to the class, or to another (not yet
created?) class?

--
Regards,
Kristofer Gafvert - IIS MVP
http://www.ilopia.com - When you need help!
"Tom" <nf*@nospam.com > wrote in message
news:eg******** *****@TK2MSFTNG P11.phx.gbl...

"Kristofer Gafvert" <kg******@NEWSi lopia.com> wrote in message
news:u%******** ********@TK2MSF TNGP09.phx.gbl. ..
Use a static method when the method does not belong to a specific
object.
What if the class is a singleton?

For instance, I have a WinForm application that does not allow multiple
instances of the application to run on the same workstation. This
application uses a Business Object singleton that caches global variables,
such as a reference to a DataSet that can be used by multiple MDI child
forms.

It doesn't seem particularly useful or efficient to create a new instance
every time I want to use one of the methods in this singleton.

Nov 16 '05 #5
IMO, statics are simplified equivalents of singletons. They grow from
globals - functions or variables. There is not much more.
I would suggest statics
- when you need global method, property or function
- when you need global variable
- when you need singleton, which exists during whole application lifetime

I found it easier to code singletons using statics. Excellent examples -
such classes like SystemInformati on or Environment.

HTH
Alex

"Tom" <nf*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
This is not intuitivelly clear.

Nov 16 '05 #6

"Ben Dewey" <be*******@scie ntiae.com> wrote in message
news:eW******** ******@tk2msftn gp13.phx.gbl...

[snip]
Basically is all boils down to memory usage. a static method is shared,
where as a non-static method gets created for every instance of the class.
Hope this makes sense?


This is not correct. The code is always shared whether it's static or not.
You're thinking of data, there's only one instance of static data,
non-static data is created for every instance.

John Vottero
Nov 16 '05 #7
My fault, that is what I was trying to get at.

Thanks for clearing that up.

"John Vottero" <Jo**@mvpsi.com > wrote in message
news:Os******** ******@TK2MSFTN GP10.phx.gbl...

"Ben Dewey" <be*******@scie ntiae.com> wrote in message
news:eW******** ******@tk2msftn gp13.phx.gbl...

[snip]
Basically is all boils down to memory usage. a static method is shared,
where as a non-static method gets created for every instance of the class. Hope this makes sense?

This is not correct. The code is always shared whether it's static or

not. You're thinking of data, there's only one instance of static data,
non-static data is created for every instance.

John Vottero

Nov 16 '05 #8
Tom
Should I make all the methods in a singleton static?

"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:es******** ******@TK2MSFTN GP11.phx.gbl...
IMO, statics are simplified equivalents of singletons. They grow from
globals - functions or variables. There is not much more.
I would suggest statics
- when you need global method, property or function
- when you need global variable
- when you need singleton, which exists during whole application lifetime

I found it easier to code singletons using statics. Excellent examples -
such classes like SystemInformati on or Environment.

HTH
Alex

"Tom" <nf*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
This is not intuitivelly clear.


Nov 16 '05 #9
Singleton idea was to create a pattern, which allows you to have only one
instance of specific object during application lifetime. Static is object
(function) independent of any objects which might exist or not during
application lifetime. Statics exist and function always. So, there is some
difference - however, not very important for me. People fought with issues
with globals and invented singletons.

As about your question - general answer for general question - it depends.
If your singleton is providing global functionality - why not? If you want
some methods to be unavailable when singleton doesn't exist - might happen -
then no.

HTH
Alex

"Tom" <nf*@nospam.com > wrote in message
news:eE******** ******@tk2msftn gp13.phx.gbl...
Should I make all the methods in a singleton static?

"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:es******** ******@TK2MSFTN GP11.phx.gbl...
IMO, statics are simplified equivalents of singletons. They grow from
globals - functions or variables. There is not much more.
I would suggest statics
- when you need global method, property or function
- when you need global variable
- when you need singleton, which exists during whole application lifetime
I found it easier to code singletons using statics. Excellent examples -
such classes like SystemInformati on or Environment.

HTH
Alex

"Tom" <nf*@nospam.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
This is not intuitivelly clear.



Nov 16 '05 #10

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

Similar topics

13
10708
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. For example if you have an Employee class rather then instantiating an instance you call a static method 'GetEmployees' and it returns a List of Employee objects. I'm looking for what other people are doing and if you feel this is a good or bad...
3
1987
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one from Guido: "The new descriptor API makes it possible to add static methods and class methods. Static methods are easy to describe: they behave pretty much like static methods in C++ or Java." http://www.python.org/2.2.3/descrintro.html
8
52753
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind it. thanks, Steven
3
2088
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
9
4154
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I had to add a call to a web service, which in
4
2042
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when changing because they are not thread-safe)." Can someone please elaborate on the pros and cons of using static methods? (In VB they are called Shared methods.) The MS DAAB uses Shared methods. This means that all Data Access calls go through...
15
2155
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data access layers, should I use static methods? Or, should I code the classes so that they have to be instantiated? I've seen examples of both, and I'm not sure which is the "preferred" or "best" way. Thanks.
12
3572
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
7
1996
by: intrader | last post by:
I have the following small classes: //----------------code--------------- using System; using System.Collections.Generic; using System.Text; namespace ValidatorsLibrary { public class ValidatorBase {
0
8459
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
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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
8570
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
8650
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...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
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
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1779
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.