One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.
So in a class Car you might have a method:
double convertMpgToKpl(double mpg)
...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):
void setMileage(double mpg)
...can't be static since it's inconceivable to call the method before any Car has been constructed.
(By the way, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g.:
Car theMoreEfficientOf(Car c1, Car c2)
Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.
One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.
So in a class Car you might have a method:
double convertMpgToKpl(double mpg)
...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):
void setMileage(double mpg)
...can't be static since it's inconceivable to call the method before any Car has been constructed.
(By the way, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g.:
Car theMoreEfficientOf(Car c1, Car c2)
Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.
Define static methods in the following scenarios only:
- If you are writing utility classes and they are not supposed to be changed.
- If the method is not using any instance variable.
- If any operation is not dependent on instance creation.
- If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
- If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
object oriented - Why have private static methods? - Software Engineering Stack Exchange
What is the static method? - Oracle Forums
Can a static method be overridden in Java?
Can a static method be overridden in Java?
Videos
So I searched up the definition of static
โIn Java, a static method is a method that belongs to a class rather than an instance of a class. The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that object of a class.โ -Techopedia This definition almost gave me a stroke ๐
I was wondering if it just means that creating a static method (just attaching the keyword static to a method declaration) means I can just use it without creating an object? And so when I want to use the method I donโt have to write like Object.methodName() I can just call it/use it directly like methodName()??? Similar to how weโve been calling and using methods in procedural style programming where you just have the static main method and then was able to casually call any other methods youโve created down below?
Is that all there is the static keyword? A method you can use without having to create an object and not needing to write Object.methodName() and just call it directly like methodName()???
Iโve trying to wrap my head and revisit this term.
The characteristic of being static is independent of the visibility.
The reasons that you will want to have a static method (some code that does not depend on non-static members) will still be useful. But maybe you don't want anyone/anything else to use it, just your class.
A fairly common reason (in Java) would be for initializing immutable field variables in a constructor by using a simple private static method to reduce constructor clutter.
- It is
private: external classes should not see it. - It is
static: it can perform some operation, independent1 of the state of the host class.
A somewhat contrived example follows...
eg:
public class MyClass{
private final String concatenated;
public MyClass(String a, String b){
concatenated = concat(a,b);
}
public String getConcatenated(){
return concatenated;
}
/**
* Concatenates two Strings as `s1---s2`
**/
private static final String concat(String s1, String s2){
return String.format("%s---%s", s1, s2);
}
}
1 Assuming it has no interaction with other static variables.