Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« Marking Time | Main | Don’t Forget Memory of a Goldfish »
Tuesday
May102011

Static on the Radio

HypnoCube2

I was doing a C# revision lecture yesterday and I was talking about static class members. I mentioned the fact that every year some students write “Static means that the value of the variable cannot be changed” in the exam, which produces an anguished cry from me and zero marks. I was trying to think of a better way to explain what static really means. And I thought about the radio. If you turn on your am radio and tune it away from a station you will hear static. And that static hiss is always there. It has been there since before radios were invented and it will be there until the universe cools right down. Static class members are a bit like this. They are always there. They exist whether you make an instance of the class or not. Static in this situation doesn’t mean “can’t change” it means “always there”.

We use them in programs when we want a data member that is part of a class but not part of each instance. For example, in a bank we might have loads of accounts, but the minimum amount you can withdraw, the maximum size of the balance we can have and the minimum age for an account holder are all relevant to accounts, but not stored in each account instance. Static works well in this situation.

Reader Comments (5)

Nice point of view, specially the "always there" thing.
May 10, 2011 | Unregistered CommenterBreno
Nicely explained... You sir are made for teaching :)
May 11, 2011 | Unregistered CommenterGeorge
I am rather curious as to whether the use of static variables is in the strict spirit of Object Orientated development. It seems a bit of a cheat, but maybe I don't understand OO propepely. I am actuaally find myself using Static variables when I cannot get visiibility between across instances and other classes. is that lazy ? - And now even more so within , WP7 Silverlight Apps, to share data betwen pages though the use of static variables in App.Cs. Seems a bit lazy just like the lazy hack of using the Global variables in Fortran.

And then we have Singletons, which is a pattern, so that must be good. But a simple static variable seems to do the job anyway.
May 11, 2011 | Unregistered CommenterJules
How about thinking of static as static caravans? They're always there too!

I think static is a more advanced idea that gets introduced to programmers too early just to make something work, and really you have to understand object properly before you can appreciate what static really means. For example, I'd like to hope that at the end of the month my salary is always there in my bank account, but it's clearly not an appropriate candidate for a static variable.

I prefer to call static part of the class and not of the instance of the class, but to understand that you have to know what a class is, and what an instance is.

@Jules, you seem to have a reasonable appreciation of classes and what static means, and you're also right in that you can abuse static to achieve things that would better be done in other ways. You can abuse static to make global variables, and a singleton is kind of a way to make one naughty little global variable to give access to a lot of other variables with on global instance. There are places where singletons are appropriate, but they can also be abused as ways to get data to places in a lazy way. There are pros and cons with singletons too. Most of them are to do with things like exactly when singletons are created and destroyed, and thread safety. That isn't to say all singletons are bad, but before you use one you need to consider if it is appropriate, or if it's just laziness.
May 12, 2011 | Unregistered CommenterSiG
What good comments. Although I'm not convinced about the idea of making things static that you want to share across pages. I cover how to do this in the Windows Phone Blue Book (subtle plug) which you can find on the Demos page. (must put a copy in Downloads as well).

I think what you have to consider here is really an issue of risk. Your customer will not care if you use static, or singletons, or double overhead underarm tripple cambered and cantelevered bridge classes. As long as the program works. But by making things static and generally easy to get at and meddle with, you are increasing the risk that someone will use something in a way you hadn't planned, and cause your program to become unreliable. For me, the fewer risks I run, and the easier it is for me to manage the use of data, the better. And I regard static data as a risk I need to be aware of.
May 12, 2011 | Registered CommenterRob

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.