I am all for static variables if you keep using them regularly and the values never change if you make them final.
Why do things the difficult way?
That is what static variables are all for.
Basically what they do is provide a generic access point you can access from any context(static, program flow, external).
You are basically stating the front door is here and it's yellow. Someone peeking from the outside will see the door which is yellow. Someone on the inside walking will see the door and that it's yellow.
Someone in a room can look into the hallway and see that it's yellow.
And if you paint it red, it will be clearly visible to everyone.
Also, there will always be 1 instance throughout the ENTIRE program with the same value. which saves memory.
take for example
class test {
public int ten = 10;
public test() {
}
}
Every time you make a new test()
an integer memory space will be assigned for the ten variable. So if you have 10 test instances you will have 10 seperate integers all holding the same value.
if you have this
class test {
public static int ten = 10;
public test() {
}
}
and you have ten test instances, you will only have one integer instance ten. This saves memory assignment, garbage collection. Although I only advice this for persistent lists/variables that do not change and you can afford to keep in memory indefenately. Don't do this with every variable. Do this for large things like an image that you re-use over and over. No use to keep the same image in memory multiple times.
When I originally wrote my answer I didn't know how static variables really worked. I got static final
and static
mixed up. On static variables you can assign new values. static final are immutable. those can't be changed.