1,105,214 Community Members

Static vs Non-Static Methods

Member Avatar
sam63
Newbie Poster
13 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What is the difference? Is one better than the other? How do you choose between the two?
Why does one not need to instantiate when calling a static method?

Member Avatar
scru
Posting Virtuoso
1,624 posts since Feb 2007
Reputation Points: 909 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 16 [?]
Featured
 
2
 

Well I can't conclusively say that one is better, because they serve different purposes.

Are you familiar with OOP? In OOP, static objects or members of a class that can be accessed directly from the class, while non-static members can only be accessed from the instance it belongs to.

C# follows a similar principle for the methods. The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance. That is why instatiating needs to be done for instance methods, while for static methods it's just not needed, and furthermore impractical (see below).

In OOP, static variables are used for values which cannot be stored by an instance variable. Example: supposed you wanted to keep a count of how many instances of a class exists? How would you store that in a single instance?

The methods use a similar principle. They should be used for procedures for which it is impractical to do within an instance of a class. I tend to use them for broad procedures (not a technical term), meaning those that do not require me to instantiate an object. Example, adding two parameters. (This usage may or may not be correct, but I believe it is)

However, if you wanted to add two properties of an object, the method cannot be static, because as you would soon realize, static methods cannot access instance methods or variables within a class. Of course that makes sense because that static method would not know which instance of the class the get these from unless it were told, since it is not part of an instance itself)

For the sake of no further complicating things, I'll stop here. Let me know if you misunderstood anything.

Member Avatar
sam63
Newbie Poster
13 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Are there methods that can be applied statically and non-statically (via instantiation) in the same program?

Ssam

Member Avatar
scru
Posting Virtuoso
1,624 posts since Feb 2007
Reputation Points: 909 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

nope, as far as I know

Question Answered as of 6 Years Ago by scru
Member Avatar
chakrimsis
Newbie Poster
2 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What is the difference? Is one better than the other? How do you choose between the two?
Why does one not need to instantiate when calling a static method?

Once u define the Static Variables it will remains the value upto u close the application
means it scop of this static varible is upto application exist
non-static varible will lose the value which is stored in the non-static varible
its scope is upto that loop only
and u need to explicit ly gargabe collect that the static variable
static variable can be accessesble by the calss name not with the object

Member Avatar
sathish@daniweb
Newbie Poster
2 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I can understand the purpose of static members .. but couldnt get the purpose of static methods.. I need to know in which situation we will use static methods..Thanks

Well I can't conclusively say that one is better, because they serve different purposes.

Are you familiar with OOP? In OOP, static objects or members of a class that can be accessed directly from the class, while non-static members can only be accessed from the instance it belongs to.

C# follows a similar principle for the methods. The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance. That is why instatiating needs to be done for instance methods, while for static methods it's just not needed, and furthermore impractical (see below).

In OOP, static variables are used for values which cannot be stored by an instance variable. Example: supposed you wanted to keep a count of how many instances of a class exists? How would you store that in a single instance?

The methods use a similar principle. They should be used for procedures for which it is impractical to do within an instance of a class. I tend to use them for broad procedures (not a technical term), meaning those that do not require me to instantiate an object. Example, adding two parameters. (This usage may or may not be correct, but I believe it is)

However, if you wanted to add two properties of an object, the method cannot be static, because as you would soon realize, static methods cannot access instance methods or variables within a class. Of course that makes sense because that static method would not know which instance of the class the get these from unless it were told, since it is not part of an instance itself)

For the sake of no further complicating things, I'll stop here. Let me know if you misunderstood anything.

Member Avatar
Diamonddrake
Master Poster
721 posts since Mar 2008
Reputation Points: 396 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 8 [?]
 
0
 

static methods are used when you will need to access a method from many different classes or forms, you wouldn't want to create an object every time you needed that method. and you certainly wouldn't want to retype or copy and paste the same method into every class you needed it in.

an example would be the static method "Show" from the static class MessageBox.

when you need a messagebox, you just call a static method to show it. ex.

System.Windows.Forms.MessageBox.Show("some message");

if it weren't static, you would first have to create an instance of the class that contains it it would be like

System.Windows.Forms.MessageBox MB = new System.Windows.Forms.MessageBox();

MB.Show("some Message");

now wouldn't that suck?

it can have other purposes but that's the deciding factor for me.

Member Avatar
sathish@daniweb
Newbie Poster
2 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

That make sense..Thanks dude..

static methods are used when you will need to access a method from many different classes or forms, you wouldn't want to create an object every time you needed that method. and you certainly wouldn't want to retype or copy and paste the same method into every class you needed it in.

an example would be the static method "Show" from the static class MessageBox.

when you need a messagebox, you just call a static method to show it. ex.

System.Windows.Forms.MessageBox.Show("some message");

if it weren't static, you would first have to create an instance of the class that contains it it would be like

System.Windows.Forms.MessageBox MB = new System.Windows.Forms.MessageBox();

MB.Show("some Message");

now wouldn't that suck?

it can have other purposes but that's the deciding factor for me.

Member Avatar
DdoubleD
Posting Shark
984 posts since Jul 2009
Reputation Points: 315 [?]
Q&As Helped to Solve: 235 [?]
Skill Endorsements: 5 [?]
 
0
 

when you need a messagebox, you just call a static method to show it. ex.

System.Windows.Forms.MessageBox.Show("some message");

I know this is solved, but I just wanted to add that I often use static methods to test out a class I have written: eg. public static void Test() , then as DiamondDrake has pointed out with the example usage of MesageBox.Show() , you let the method, Test() in my case, take care of the details related to the class including instantiating it.

Cheers!

Member Avatar
sgavriel
Newbie Poster
1 post since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

We use the static method in a server class so all static methods and fields would be the common data for server instances. The instances are per client connection.
The static fields and methods are used for the common actions and info.

What do you think about this model?

--------
Thanks
Sharon

Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

Welcome sgavriel.

We appreciate your help. Have you ever noticed that the current thread is three years old? Please do not resurrect old threads. Take a look at forumrules.
Thread Closed.

MuhammadSaad
Newbie Poster
1 post since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Thanks. Its really a nice article. It helped me very much... Thenks again.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article