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.

For example,


final int i =0;

Frankly, in java, we always combine static and final together to make it more effective.

For example,


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 🙂

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
DANIEL
13 years ago

please help me to know Java programming very well