views:

96

answers:

4

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.

???

My encounter with this static keyword occurred when I defined a variable public int abc. When the value of this variable changed withing the brackets of while, or within the brackets of if, or within the brackets of methods, these changes were not valid, were not reflected, were not know outside the brackets. So just a hit trial. I made the variable static and all problems solved.

But why??

+5  A: 

The difference between a static and a non-static member (not variable) is that a static member is unique over the whole runtime of a program (i.e. there is only one static member instance) whereas a non-static member is associated with an object instance (i.e. there is a member instance for each instance of the corresponding object). This is in a bit more words what the definition says.

How this all applies to what you wrote in regard to changes not reflected im not too sure - maybe you should post the corresponding code.

inflagranti
+3  A: 

I did not understand what you meant but
static variables are variables at the class level - they do not belong to an instance of an object but to the class itself. They can be used and accessed without instantiating any instance.
An instance (non static) variable on the other hand belongs to the instance itself.

Itay
+1  A: 

It's probably because you're creating multiple copies of the class, and each class has its own values for that member.

By change it to static, all instances share the same copy of the member variable.

The behavior that you're observing probably indicates a problem in your while and if expressions. If you post some sample code, I might be able to help further.

Mike Hanson
+2  A: 

If you have a class:

public class Car {
   public static readonly int Wheels = 4;
   public static int Count {get;set;}
   public string Make {get;set;}
   public string Model {get;set;}
   public int Year {get;set;}

   public Car() { Car.Count = Car.Count + 1; }

   public string SoundOff(){
       return String.Format("I am only 1 out of {0} car{1}", 
              Car.Count, (Car.Count > 0 ? "s" : String.Empty));
   }
}

Then, every time you create a car, the count will increase by one. This is because the Count property belongs to the Car class, and not to ever object you've created.

This is also useful because every car can have knowledge of the Car.Count. So, if you created:

Car sportster = new Car { 
   Make="Porsche", Model="Boxster", Year=2010  };
sportster.SoundOff(); // I am only 1 out of 1 car

You can do other processing and Count will be known to all objects:

Car hybrid = new Car { Make="Toyota", Model="Prius", Year=2010 };
hybrid.SoundOff(); // I am only 1 out of 2 cars
sportster.SoundOff(); // I am only 1 out of 2 cars

So, in other words, you should use static when you want something to:

  • Be accessible at the class-level so all objects know of it (Car.Count instead of hybrid.Count)
  • Represent the class and not the object (Car.Wheels won't change)

There are other reasons to use static, like Utility classes, extension methods, etc. But this should answer your question about the MSDN wording.

Jim Schubert