static methods

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
Are there any disadvantages to making static methods for classes instead of just public?
 
Not so much disadvantages as just differences. A static method is like a global function. A public method is intended to do some action on a specific instance of a class.

So a Car object might have a method named Start() but you wouldn't make it static because you only start a specific car (for example).

The DateTime object has a static method IsLeapYear(int year) that returns true or false whether the year is a leap year. It's static because you don't want to have to create a DateTime object just to call that function.

Whether your objects have static or just public methods is entirely up to you - it's just whatever makes sense.

-Nerseus
 
Static methods are more convenient and eisier to use but I have seen that when I try to create a thread for the method I cannot. Or at least I don't know how. I will post a new post right now about threads.
 
Nerseus...
This brings me to questions that bugged me for a while:

How does the garbage collector handle static vars/methods? Looking at memory resources, is it good or bad to use statics?
Any guidelines?

Thanks!
 
Again, it's not good or bad to do anything if it's needed. But you probably don't need very many static variables, in general (I've never had the need for more than 2 or 3). Static methods only take up the memory for the compiled instructions of course, just like any other code.

I don't remember offhand how the garbage collector treats static variables - I would imagine it treats them like any other chunk of memory.

-Nerseus
 
Back
Top