What the difference given they all exist 'once per class'.
When should I use one over another, i.e. for what purpose?
I use Ruby.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
The differences are well-defined, and you can look them up easily. Here's a quick (language-agnostic) summary:
As to which one to choose: My advice is to avoid class-level attributes and singletons as much as possible. Both are globals in disguise, and although there might be appropriate uses for them, most real-world applications are in fact inappropriate, introduced for "convenience" at one point, and a source of major headaches later on. In most situations, a better alternative is to create a regular type to describe the state that you would otherwise put in a class attribute or singleton, and just pass it around. This way, you can control exactly who gets to modify the state (and who doesn't), and should you ever decide that you do need more than one instance, you can just create one. The hassle of carrying the state object around is minimal in comparison.
Class constants, by contrast, are fine - they are constants, so there is no risk of unexpected changes. They are just values that you can access from anywhere, and there is nothing wrong with that (as long as the code that uses them doesn't break if you change the value).