2

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??

2
  • 1
    You are having a load of paragraphs there... do not constantly hit the enter key ;) it hurts reading the actual question. Commented Jun 20, 2010 at 12:36
  • Changes to an object's variable should always be valid within that object, no matter what type of block you're passing it through within that object. If, however, you're passing that variable to another method of another object, you'll have to pass it by reference to be accessible when that method finishes execution. You'll also need to be careful not to hide member variables by local variables of the same name. Commented Jun 20, 2010 at 13:19

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

3

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.

1 Comment

I apologize for the poorly named SoundOff method :) It's the military cadence, "Sound-off: 1, 2"... not a method which turns the sound of a car off (available only on hybrids). I'm usually against crafty method signature names.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.