Static vs Non-Static Methods

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 13
Reputation: sam63 is an unknown quantity at this point 
Solved Threads: 0
sam63 sam63 is offline Offline
Newbie Poster

Static vs Non-Static Methods

 
0
  #1
Nov 16th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Static vs Non-Static Methods

 
1
  #2
Nov 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 13
Reputation: sam63 is an unknown quantity at this point 
Solved Threads: 0
sam63 sam63 is offline Offline
Newbie Poster

Re: Static vs Non-Static Methods

 
0
  #3
Nov 16th, 2007
Are there methods that can be applied statically and non-statically (via instantiation) in the same program?

Ssam
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Static vs Non-Static Methods

 
0
  #4
Nov 16th, 2007
nope, as far as I know
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: chakrimsis is an unknown quantity at this point 
Solved Threads: 0
chakrimsis chakrimsis is offline Offline
Newbie Poster

Re: Static vs Non-Static Methods

 
0
  #5
Nov 24th, 2007
Originally Posted by sam63 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: sathish@daniweb is an unknown quantity at this point 
Solved Threads: 0
sathish@daniweb sathish@daniweb is offline Offline
Newbie Poster

Re: Static vs Non-Static Methods

 
0
  #6
Sep 9th, 2009
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

Originally Posted by scru View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 357
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 45
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: Static vs Non-Static Methods

 
0
  #7
Sep 9th, 2009
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.
  1. 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

  1. System.Windows.Forms.MessageBox MB = new System.Windows.Forms.MessageBox();
  2.  
  3. MB.Show("some Message");

now wouldn't that suck?

it can have other purposes but that's the deciding factor for me.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: sathish@daniweb is an unknown quantity at this point 
Solved Threads: 0
sathish@daniweb sathish@daniweb is offline Offline
Newbie Poster

Re: Static vs Non-Static Methods

 
0
  #8
Sep 11th, 2009
That make sense..Thanks dude..

Originally Posted by Diamonddrake View Post
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.
  1. 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

  1. System.Windows.Forms.MessageBox MB = new System.Windows.Forms.MessageBox();
  2.  
  3. MB.Show("some Message");

now wouldn't that suck?

it can have other purposes but that's the deciding factor for me.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: Static vs Non-Static Methods

 
0
  #9
Sep 11th, 2009
Originally Posted by Diamonddrake View Post
when you need a messagebox, you just call a static method to show it. ex.
  1. 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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 19938 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC