473,395 Members | 1,379 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,395 software developers and data experts.

When i want to use a static method?

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.
Fernando
Nov 17 '05 #1
8 2040
I use static methods when I need to get something that is relevant to
that class but I don't want to create an instance everytime to get this
information. With a static method you cannot access
methods/properties/variables from the class. The only variables
available are within the static method you are calling.

Nov 17 '05 #2
Typically Helper and Utility methods are good candidates. Often times as
well, you may want to create Static as well as instance objects. Looking to
the framewokr, there's the File and the FileInfo, the Directory and
directoryInfo. Similarly, check out the Data Access Application Block - all
of the SqlHelper methods are static - and as a rule of thumb, anything you'd
name xxxHelper usually is a great candidate for being implemented as a
static.

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Fernando Lopes" <fernandomattarlopes@[remove]msn.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
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.
Fernando

Nov 17 '05 #3
I think that you should also make a distinction between when to create
_public_ static methods, and when to create _private_ static methods.

All of the information given here so far is valid for public static
methods. If your class is exporting a static method, it's usually
because it's functionality that belongs with your class, but for which
you don't want your callers to have to create an object just to use it.
For example, most Parse methods are static, because you don't want your
caller to have to create a throw-away object just to parse a string to
make the object they really want. To illustrate:

MyClass parsedObject = MyClass.Parse(inputString);

is better than

MyClass parsedObject = new MyClass().Parse(inputString);

because the Parse method doesn't need anything from a particular
MyClass in order to do its job (it just needs the string), and why
should your caller create a throw-away MyClass (new MyClass()) in order
to use the method?

_Private_ static methods come with different considerations.

Whenever you make an instance private method, you're giving that method
access to all of the fields in your class. Sometimes you don't need
that. Sometimes you just need a little "helper" method that does a
small job. Rather than give it access to your class's state, you can
make it static and pass it everything it needs in order to do its job.

This is easier to read and easier to maintain. A static private method
says, "You don't need to worry that somewhere inside my code I use some
fields of this class... or, even worse, that somewhere buried in my
code I _change_ some fields in this class. The only information I need
is in my parameter list. The only information I modify is in my
parameter list and in my return value."

Not only that, but the static private method can't call any instance
methods, so it can't modify an object in any other way.

In other words, static private methods come with many fewer assumptions
that private instance methods. For that reason they're attractive.
However, if you have to pass a dozen fields to a private static method,
then it should probably be an instance method. There's a balance to be
struck: if an instance method needs only one or two instance fields,
consider making it static. If a static method needs a dozen instance
fields passed to it, consider making it an instance method.

Nov 17 '05 #4
"DKode" <dk****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I use static methods when I need to get something that is relevant to
that class but I don't want to create an instance everytime to get this
information. With a static method you cannot access
methods/properties/variables from the class. The only variables
available are within the static method you are calling.

Not quite right, you can also access static class variables (and consts) or
static properties
Nov 17 '05 #5
KH
> I know this methos don't want to be initialized

All methods are static in a sense - even instance methods. The logic of
instance methods does not get copied every time an instance of its class is
created, it lives in one place and a pointer to the instance is passed to
that method - that's what the "this" pointer is, it's just done for you.

Basically what happens is that the compiler sticks a pointer to an instance
of the class as the first argument. So for class Foo, you define a method:

int Bar(int i) { return(this.x + i); }

and the compiler turns it into this:

int Bar(Foo f, int i) { return(f.x + i); }

If a copy of each method was made for each object allocation there would be
huge and unnecessary overhead; think of a class like String with a ton of
methods being copied all over the place all the time.
"Fernando Lopes" wrote:
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.
Fernando

Nov 17 '05 #6
Good info Bruce.
Also, thanks for taking the time to write that long explanation.

--
info@donotspam dowhileloop.com
http://www.dowhileloop.com web development
http://publicjoe.dowhileloop.com -- C# Tutorials

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I think that you should also make a distinction between when to create
_public_ static methods, and when to create _private_ static methods.

All of the information given here so far is valid for public static
methods. If your class is exporting a static method, it's usually
because it's functionality that belongs with your class, but for which
you don't want your callers to have to create an object just to use it.
For example, most Parse methods are static, because you don't want your
caller to have to create a throw-away object just to parse a string to
make the object they really want. To illustrate:

MyClass parsedObject = MyClass.Parse(inputString);

is better than

MyClass parsedObject = new MyClass().Parse(inputString);

because the Parse method doesn't need anything from a particular
MyClass in order to do its job (it just needs the string), and why
should your caller create a throw-away MyClass (new MyClass()) in order
to use the method?

_Private_ static methods come with different considerations.

Whenever you make an instance private method, you're giving that method
access to all of the fields in your class. Sometimes you don't need
that. Sometimes you just need a little "helper" method that does a
small job. Rather than give it access to your class's state, you can
make it static and pass it everything it needs in order to do its job.

This is easier to read and easier to maintain. A static private method
says, "You don't need to worry that somewhere inside my code I use some
fields of this class... or, even worse, that somewhere buried in my
code I _change_ some fields in this class. The only information I need
is in my parameter list. The only information I modify is in my
parameter list and in my return value."

Not only that, but the static private method can't call any instance
methods, so it can't modify an object in any other way.

In other words, static private methods come with many fewer assumptions
that private instance methods. For that reason they're attractive.
However, if you have to pass a dozen fields to a private static method,
then it should probably be an instance method. There's a balance to be
struck: if an instance method needs only one or two instance fields,
consider making it static. If a static method needs a dozen instance
fields passed to it, consider making it an instance method.

Nov 17 '05 #7
Bruce Wood <br*******@canada.com> wrote:
This is easier to read and easier to maintain. A static private method
says, "You don't need to worry that somewhere inside my code I use some
fields of this class... or, even worse, that somewhere buried in my
code I _change_ some fields in this class. The only information I need
is in my parameter list. The only information I modify is in my
parameter list and in my return value."


Unless there are static fields in the class of course - and a static
method can call private methods (or modifiy private fields) on any
instances of the class it's able to get hold of (whether parameters,
static fields or whatever).

Often it's nice to have a static method which takes two instances of
the class just because it's not specifically applicable to one instance
"more" than the other instance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
> Unless there are static fields in the class of course - and a static
method can call private methods (or modifiy private fields) on any
instances of the class it's able to get hold of (whether parameters,
static fields or whatever).

Agreed. Nonetheless, I think it's easier to maintain a private method
that takes one or two extra arguments and is static than a private
instance method that uses only one or two instance fields, because you
don't really know what the latter is doing without examining the code.

Nov 17 '05 #9

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

Similar topics

7
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
2
by: Ge Cong | last post by:
I have a class class MyClass { private: static double a; static void computeA(){ a =....; }
4
by: Terry | last post by:
There are a number of things about using unmanaged resources in Windows Forms programming that is unclear to me. In C++, if you loaded an icon resource using "ExtractIcon()", the resource was...
17
by: Tom | last post by:
This is not intuitivelly clear.
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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...

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.