That's the best way.
(public/private) static final TYPE NAME = VALUE;
In this way, TYPE refers to the type of variable, and NAME usually consists of uppercase letters and _. For VALUE, you can put a constant value.
Note that a variable cannot change its value if it is declared final, but for an object it cannot point to another object. For example,
public static final Point ORIGIN = new Point(0,0);
public static void main(String[] args){
ORIGIN.x = 3;
}
In this case, ORIGIN.x=3 works normally and the value of the point is (3,0).
© 2024 OneMinuteCode. All rights reserved.