when the static keyword is used it makes the method or variable available to the program without having to instantiate an object from the class
i get this.
but when is the right time to apply this...heres my example and you can tell me if its ok or should i be doing something else
i have a class called VariousFunctions
and in this class I have a method called
it basically prints out the selections available but also handles user input and logic for the menu system
is this perfectly ok to make a static method?
or are there caveats or pitfalls when programming like this...
and hes using static so i guess its ok
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.
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?
jon ninpoja wrote:so when would you use a static method and when would you use an enum? or am i missing something here?
I think you're missing something. Those are two separate questions: "When would you use a static method" and "When would you use an enum". There's no particular connection between the two.
jon ninpoja wrote:do they not both deal with constants?
I don't think so. Could you elaborate on why you think a static method is, or deals with, a constant?
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.
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.
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.
Les Morgan wrote:. . . unless you know . . . what they do for the instance you want . . .
Instance variable: It will be different from object to object and object's property while static variable is Class's property.
Static function: Used to do some utility task. Can be called without any object declaration.
Instance function: Need object to call this function.
static or instance depends on your uses .
Many people believe there should be no such thing as a static variable. Only constants.
Jack Ramon wrote:Static variable: When you need something that will be used through out the application and every instance need to know the variable. . . .
That is very vague. But pure functions usually are marked static.
Static function: Used to do some utility task. . . .
In an object language like Java®, everything should default to being instance type. Only make things static as an exception to that rule with a good explanation.
static or instance depends on your uses .