Is there any constant in Java? how to declare it?
Recently, I’m been asked by my colleague (C++ programmer) regarding why java does not contains a constant keyword? Actually java do contains constant function , but it just appear as different keyword – final. A final variable in java is equal to C++ constant. The final keyword is declare before a data type, it make variable unchangeable.
final int i =0;
Frankly, in java, we always combine static and final together to make it more effective.
public static final int MAX_SIZE = 25;
P.S The static modifier causes the variable to be available without create an instance of the class.
According to java code standard advice, final variables are usually declared in ALL CAPS and Words are normally separated by underscores.
Hi, C++ guys, never say Java does not contains constant function again :)