When should I make a class static?

i have been learning how to program for some time now and one thing that still stumps me to this day is knowing when to make a method static or not.  I just don't get the point of instantiating a method if I can make it static!

None of my data access classes are static, but all of the classes that do things like convert time, etc, are static.  What is the general rule on this?  I just don't get it - any help you can give is greatly appreciated.

PS - Whether I make my time classes static or not, the same result is brought out...  No differences - I have not tried data access classes iwth a static class.

Thanks alot,

StankStank
stankstankAsked:
Who is Participating?
 
almilyoConnect With a Mentor Commented:
Say you're making an application for a College to administer it's students. Somewhere inside you'd probably have a class representing a Student, with say, a property representing the year they joined the college.

There are many students in the college, most with different start dates. You would 'instantiate a new object' based on the Student class for each one. Each of these student objects you'd create would be held in a different memory space, could be addressed seperately and have a different year of joining.

Now, let's say you need to create a set of functions to manipulate generic data in some way (like converting times). These are just libraries that you use, and they don't represent different things, so you  don't need to create a whole new version of it each time you use it - you don't need to create many instances of it.

The first type of situation is where a non-static class would be appropriate (where you need many seperate objects), the second is where a static class would be used (there is only ever one).

0
 
Arthur_WoodConnect With a Mentor Commented:
Basically, Static classes would apply for Utility type functionality.

For instance, if you want to verify that a Date value is Valid (let's say - any date that is after 1/1/2000 but before 1/1/2010 would be considered as valid)  This would be a candidate for a Static method

Public Class DateUtilties
     Public Shared Function isValidDate(byVal checkDate as Date) as Boolean

     End Function
End Class

or in C#

Public Class DateUtilties
     bool static isValidDate( Date checkDate)
       {

       }
End Class

AW
0
 
Arthur_WoodCommented:
The big difference is that with Shared (VB) or static (C#) procedures, you do NOT create an instance of the class, in order to use the shared/static procedures within the class.

AW

0
 
JimBrandleyConnect With a Mentor Commented:
Static methods can be used when the method operates only on variables passed as parameters or constant values from the class that contains it. Any method that needs to access a member variable in the containing class cannot be static.

Jim
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
All Courses

From novice to tech pro — start learning today.