Short answer: never.
Slightly longer answer: only when you are confident you are working outwith the scope of any object.
Longer answer still: only when you are confident you are working outwith the scope of any object. That includes code which has to be run before any objects are available.
Constants don't vary, so nothing happening in an object affects them, so constants are usually declared static.
Methods which are pure functions come out as 1368 in the most dubious classification of methods known to modern science; they are therefore usually made static. You can see examples of pure functions in utility classes: java.lang.Math is a good example. Note it also has constants in.
Factory methods usually have to be called before any instances are available, so they usually have to be static. You will notice many factory methods are technically pure functions.
posted 1 year ago
Enums are to do with constants too arent they?,so when would you use a static method and when would you use an enum? or am i missing something here?
Enum isn't a method as opposed to static method. Enum is in some sense a special data type, which appear to be constant (in most cases a set of constants). While static method is method which appear to be static.
There are 4 instances that I can think of where I use static:
1 -- Factories: I don't want to have to instantiate a factory just to use it.
2 -- Class Variables: they are like wormholes, they are the same throughout the universe. If you don't know what that means, then don't use them.
3 -- Interfaces: you can add static and default methods to an interface and not have to recompile the project--static and default are not the same thing.
4 -- constants: static final--they are what they are and you cannot change them anywhere.
There are probably more, but the very best thing I can say is: as a general rule of thumb, don't use them. So unless you know you really want them, what they do for the instance you want, and specifically what the side effects are in using them.
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
The declaration of constants should be self evident. Most of the time, you would use an enum for these, but sometimes you want to declare constants of some primitive or other immutable type.
By private pure functions, I mean static methods that have parameters, and return something based on the values of those parameters, and nothing else. The reason to make them private is that they should only be called from other methods in the same class, to break up those methods into smaller chunks and make them self-documenting. If you have to write a pure function that's not private, it's either because you don't control the types involved in the parameter or return types, your application design is not object oriented, or you're writing a factory method (next point).
Factory methods are a special type of pure function that can be seen as named constructors. They generally should be named something like createTypeName(Foo foo, Bar bar), parse(String string) or valueOf(Foo foo). In general, with the exception of parsing methods, they should not do much more than call a constructor or retrieve a cached value.
posted 1 year ago
Les Morgan wrote:. . . unless you know . . . what they do for the instance you want . . .
I presume you are using “instance” in the sense of “under these circumstances” rather than “instance of a class”. As I said earlier, pure functions are usually implemented as static methods.