Main Tutorials

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 🙂

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
DANIEL
11 years ago

please help me to know Java programming very well