Welcome to AspAdvice Sign in | Join | Help

Static vs Non - Static Methods

This is an often misunderstood concept but an important one for learners who are new to C++/Java/C#. 

A static method belongs to the class and a non-static method belongs to an object of a class. That is, a non-static method can only be called on an object of a class that it belongs to. A static method can however be called both on the class as well as an object of the class. A static method can access only static members. A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself). In the other case, a non-static method can only be called when the class has already been instantiated. A static method is shared by all instances of the class. These are some of the basic differences. I would also like to point out an often ignored difference in this context. Whenever a method is called in C++/Java/C#, an implicit argument (the 'this' reference) is passed along with/without the other parameters. In case of a static method call, the 'this' reference is not passed as static methods belong to a class and hence do not have the 'this' reference.

Sponsor
Published Sunday, September 17, 2006 6:36 AM by joydipkanjilal

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Static vs Non - Static Methods

Sunday, September 17, 2006 9:02 AM by shabana

i want  examples for both static and non static methods showing the difference between these methods

# re: Static vs Non - Static Methods

Sunday, September 17, 2006 9:07 AM by shabana

i want examples for both static and non static methods showing the difference between them.

# re: Static vs Non - Static Methods

Sunday, September 17, 2006 9:09 AM by shabana

i want examples for both static and non static methods showing the difference between them.

# re: Static vs Non - Static Methods

Sunday, September 17, 2006 9:10 AM by shabana

i want examples for both static and non static methods showing the difference between them.

# re: Static vs Non - Static Methods

Sunday, September 17, 2006 9:31 AM by shabana

i want examples for static and non static methods

# re:i want how insert template fields in custom controls

Wednesday, September 20, 2006 2:11 AM by bhagavanreddy

sir

  i have developing controls and i have how to insert templates fields in custom controls send me examples of that templates.

 thanks

# re: Static vs Non - Static Methods

Thursday, October 12, 2006 2:12 PM by Joydip

Shabana,

In case of a static method, the method signature is prefixed with the static keyword. The static method can be called without instantiating the class to which it belongs. Hence, a static method fn() can be called using the class name as shown below:---

c.fn()

where c is the name of a class and fn is a static method of the same class.

Please refer to my articles at ASPALLIANCE for further reference.

# re: Static vs Non - Static Methods

Friday, January 19, 2007 2:56 AM by Mayank Goyel

Hi All, I just want to say that this article is good but it has an error. This article is not completely valid for C#. In C#, the static method can not be called on an object of the class. Only class can access the static method in C#.

# re: Static vs Non - Static Methods

Sunday, February 11, 2007 11:48 PM by leprien

I have an error non-static method can not be referenced from a static context

# re: Static vs Non - Static Methods

Monday, February 12, 2007 12:16 AM by leprien

I have written moreless an exact replica of a compilable class and driver i wrote that run fine. But the replica driver will not compile

From within my static main method I call an instance method from a class that is not labeled static anywhere. The only differences between my new replica class and the old one, is the instance methods in the one who's based on driver will not compile consists of math operations on the parameters that are passed to it. The class compiles. The driver dose not. Compile error reads non-static method can not be referenced from a static context.

The 2 drivers for the replica class and original are identical. I have been over it twenty five times or more. The class method without math operations in it works just fine.

with my driver. I am about to test this by placing a few arithmetic operations in my original class with its based on driver I will see if MATH is my problem???

These are two entirely different programs.

# re: Static vs Non - Static Methods

Monday, February 12, 2007 12:19 AM by leprien

I have written moreless an exact replica of a compilable class and driver i wrote that run fine. But the replica driver will not compile

From within my static main method I call an instance method from a class that is not labeled static anywhere. The only differences between my new replica class and the old one, is the instance methods in the one who's based on driver will not compile consists of math operations on the parameters that are passed to it. The class compiles. The driver dose not. Compile error reads non-static method can not be referenced from a static context.

The 2 drivers for the replica class and original are identical. I have been over it twenty five times or more. The class method without math operations in it works just fine.

with my driver. I am about to test this by placing a few arithmetic operations in my original class with its based on driver I will see if MATH is my problem???

These are two entirely different programs.

# re: Static vs Non - Static Methods

Monday, February 12, 2007 12:21 AM by leprien

I like e-mail shawn@jetup.net

# re: Static vs Non - Static Methods

Monday, February 12, 2007 12:21 AM by leprien

I like e-mail shawn@jetup.net

# re: Static vs Non - Static Methods

Monday, February 12, 2007 12:23 AM by leprien

i would like to add this is JAVA

# re: Static vs Non - Static Methods

Thursday, June 14, 2007 11:37 AM by Thiagarajan

Hi

I have some questions on using static methods against instance methods.

Consider, i am having one static function, which has takes about 5 secs to complete its whole process.

When the caller functions makes requests to this static method continuously. First call will get processed. What will happen to the subsequent calls. Will it be placed in the Queue? If it is placed in the queue, then the response time will increase. As we have only one copy of static method in the memory, how it is handling multiple calls raised almost at the same moment.

# re: Static vs Non - Static Methods

Tuesday, July 17, 2007 3:55 PM by cs

fk

# re: Static vs Non - Static Methods

Thursday, January 24, 2008 8:32 PM by Mike

Joydip, I'm not too sure what you mean! It isn't clear in my mind...

Would it be possible to include C# examples?

# re: Static vs Non - Static Methods

Wednesday, March 12, 2008 5:53 AM by govind

give me some examples on static and non-static

# re: Static vs Non - Static Methods

Friday, May 30, 2008 2:42 AM by Sanjar

Thiagarajan,

Logically it seems that it is happening at the same time. But the system processes it step-by-step and so quick that you find it happening at the same time.

Hope that answers your question.

# re: Static vs Non - Static Methods

Wednesday, November 12, 2008 5:24 AM by Pearl

Thank you Joydip,

I got cleared about the doubt the difference between Static and non static methods. The concept about 'this' refernce in the static and non static method is very clear.

# re: Static vs Non - Static Methods

Saturday, November 15, 2008 9:55 AM by nishu

kalakki mone kalakki

# re: Static vs Non - Static Methods

Friday, May 22, 2009 12:22 PM by Johnny

Note: a static method can call a non static method via delegate

Ex:

//A Class

class StaticMethodClass

{

public delegate void delegateGeneral();

public static void StaticMethod()

{

//Static method called successfully

MessageBox.Show("Static Method Called");

//Instantiate new StaticMethodClass object to pass to delegate

StaticMethodClass staticmethodclass = new StaticMethodClass();

//This is the delagate recieving the new StaticMethodClass object and notice the non static method call

delegateGeneral delegateCallNonStatic = new delegateGeneral(staticmethodclass.CallNonStaticMethod);

//Call the method via delegate

delegateCallNonStatic();

}

//The non static method routine...

public void CallNonStaticMethod()

{

NonStaticMethodClass nonstaticmethodclass = new NonStaticMethodClass();

           nonstaticmethodclass.NonStaticMethod();

}

}

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below