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 the Author

author image
mkyong
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

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
DANIEL Recent comment authors
newest oldest most voted
DANIEL
Guest
DANIEL

please help me to know Java programming very well