• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Classes  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have an object in a program that shouldn't be duplicated, (it should only be created once), should the class that defines that object be "static"?

As an example: I have defined a LapCounter class to count the number of times a RaceCar object makes a lap around the track. I only need one LapCounter object. Should I simply define the LapCounter class as a static class?

Do you still have to use the "new" statement to create a instance of a static class to access it's member variables and methods?

As an Example: The LapCounter class defines a method named "UpdateNumberOfLaps". If LapCounter was a static class do I still have to create a LapCounter object before I can call LapCounter.UpdateNumberOfLaps from my RaceCar object?

Thanks

Landon
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want is a Singleton.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!