8 Replies - 4936 Views - Last Post: 28 June 2008 - 10:13 PM Rate Topic: -----

#1 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Static vs Instance methods

Posted 27 June 2008 - 10:11 PM

I'm rereading a tutorial from a previous class, and I found something I don't quite understand. Maybe you can help me see what this means.

From the tutorial:
"The difference between instance methods and static methods is that multiple instances of a class can be created (or instantiated) and each instance has its own separate getChoice() method. However, when a method is static, there are no instances of that method, and you can invoke only that one definition of the static method."

What does this mean, that each instance has its own separate method? There is only one getChoice() method in this program, and it's only defined one time...right?

What does it mean that you can only invoke that one defintion of the static method? How could there be more than one definition of a method? Wouldn't you simply use another method with a different name, whenever you want a different method?

Here's the class and method as written in the tutorial:

class OneMethod
{
public static void Main()
{
string myChoice;

OneMethod om = new OneMethod();

do
{
myChoice = om.getChoice();

//I omitted the code to do stuff here

}while(myChoice != "Q" && myChoice != "q"); //keep going until the user wants to quit
}

string getChoice()
{ //I omitted the code to show a menu and get user's choice

return myChoice;
}
}


This post has been edited by OliveOyl3471: 27 June 2008 - 10:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Static vs Instance methods

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Static vs Instance methods

Posted 27 June 2008 - 10:53 PM

Lets say that you have a class called "Car". This class is a blueprint for how to build a car, but it is not a car. You create instances of an object using lines like...

// Create a red car
Car redcar = new Car("red");

// Create a blue car
Car bluecar = new Car("blue");

// Create a green car
Car greencar = new Car("green");



Here I have created three variables of type "car". Each of these variables are an INSTANCE of type car. One instance is red, one is blue, and another is green. They all act as their own object. So if I call "setColor" on the greencar and change it to "pink" the red and blue cars remain unchanged. They are their own objects and hence have their own versions of the "setColor" method. The method only affects that particular instance or "copy" of the car.

Static methods mean that you don't need to create these car instances. Instead you can call methods directly using the class name. So for our car class if we had a static method called "numberOfWheels" which always returned "4" then I could use it like so...

Messagebox.Show(Car.numberOfWheels.ToString());



Notice that I didn't use the new keyword, I didn't create an instance of a car, I called the method directly using the class name "Car".

Another example of a static method is with the use of the Math class. You don't need to create instance variables of type "Math" and then call "sqrt()". Since sqrt() gets the square root of a number it can operate all on its own.

// Wrong... no need to create an instance and then call its method
Math myinstance = new Math();
myinstance.Sqrt(25);

// Right... we can simply call the method directly using the class name
Math.Sqrt(25);



I think this is where books get it wrong sometimes and confuse people. They bring in the idea of sharing when really what static methods are is methods which don't need an instance to be used. This allows you to create tons of methods which you can package up into a class. Like the Math class has Sin, Tan, Sign, Cos etc all which are static methods. The class is thus static.

Ever notice that the main() method is also static? This is because the program needs to be able to call main without the need of creating memory, making an instance, and then calling main. It calls main() directly without the need of an instance variable.

I hope this sheds some light on the whole thing. :)
Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Static vs Instance methods

Posted 27 June 2008 - 11:52 PM

View PostMartyr2, on 28 Jun, 2008 - 12:53 AM, said:

I hope this sheds some light on the whole thing. :)


Thank you. Yes, it does make sense now. You are kind to say it is the books that are wrong, though.
:P
Was This Post Helpful? 0
  • +
  • -

#4 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: Static vs Instance methods

Posted 28 June 2008 - 02:45 AM

So, Martyr, it's kind of like the difference between getting laundry washed at a Laundromat (static method) and owning your own washing machine (object instance method).
:) Am I right?

Laundromat = many people can use it to do their washing.

Your own machine = only you (or your family) use it.

Sometimes I find it easier to think in real life situations / examples.
Was This Post Helpful? 0
  • +
  • -

#5 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Static vs Instance methods

Posted 28 June 2008 - 03:01 PM

Now that you know what statics are, here is something to remember:

From "Beginning Visual C# 2005 Express Edition" by Peter Wright

Quote

If you use statics all the time, you aren't giving .NET a chance to clean up memory from unused objects (because you are not ever creating objects, just calling class methods on classes). The net result is that your program bogs down and starts to get slow. More to the point, by using statics you are forgoing all the benefits that object-oriented programming can bring you, such as inheritance and encapsulation of data and functionality.
Using statics everywhere is an easy trap to fall into if you come from a VB background, so be aware of it and try to use them sparingly.

Was This Post Helpful? 1
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Static vs Instance methods

Posted 28 June 2008 - 04:33 PM

View PostFootsie, on 28 Jun, 2008 - 02:45 AM, said:

So, Martyr, it's kind of like the difference between getting laundry washed at a Laundromat (static method) and owning your own washing machine (object instance method).
:) Am I right?

Laundromat = many people can use it to do their washing.

Your own machine = only you (or your family) use it.

Sometimes I find it easier to think in real life situations / examples.


You almost have it. For your analogy it is like having Laundromat.washclothes() as static and for instances it is like you have your own personal replica of the laundromat building in your backyard. It is an Instance of a laundromat class.

But for the most part you got it. :)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5775
  • View blog
  • Posts: 12,587
  • Joined: 16-October 07

Re: Static vs Instance methods

Posted 28 June 2008 - 06:03 PM

View PostFootsie, on 28 Jun, 2008 - 05:45 AM, said:

Sometimes I find it easier to think in real life situations / examples.


I like this game. You'll forgive me if I give it a go...

An object method is a movie on a DVD. The movie is loaded upon individual request. You can watch it when you like. You can manipulate many aspects of how it plays, pause, and skip, and control the volume. You have the ability to "override" the standard behavior of just watching the movie beginning to end.

A static method is a movie in a theater. It's available in a standard way in a standard place. All consumers of the movie are sharing the exact same copy of the movie. There is no ability of an individual process, um, person, to change the behavior of the movie.
Was This Post Helpful? 0
  • +
  • -

#8 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Static vs Instance methods

Posted 28 June 2008 - 08:33 PM

Now I understand why I get an error if there is a method that is not static, but has no instance.

A static method does not change and will always have the same effect each time you use it. An object (instance) method uses objects to change its behavior, or at least its effect. Each object (variable) has its own version or usage of the method, so although the method itself does not change, the result returned from it, or what you can do with it can change depending on the object(s) associated with it.

Therefore, it is more functional to use methods that have instances. For example, to get the area of a rectangle. If you want to get the area of the same rectangle, you could use a static method, but if you wanted to get the area of a different rectangle, then you would need an instance method, so you can pass different data (sizes of the sides) into the method each time you need it.

Is that right?
Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Static vs Instance methods

Posted 28 June 2008 - 10:13 PM

Yeah you pretty much have it. The best way I like to remember it is with the whole Math.sqrt() example earlier. Sqrt() is static because what does it mean to have an instance of Math? How is math an object? Can you hold an instance of math in real life? no.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1