Core Java Tutorials - Creating Constants in Java, How to Create Constants in Java


Creating Constants in Java, How to Create Constants in Java


A simple variable's value can be change during the program execution but a constant value can't be change after it's creation time.

A constant must be declared and initialized in the same statement. In other programming languages like C and C++, we use a keyword const to declare a constant same as the word final is a Java keyword for declaring a constant. For example, you can declare PI as Constant like this-

final double PI=3.14159;

Benefits for declaring constants


There are three benefits of using constants:

(1) you don’t have to repeatedly type the same value.

(2) if you have to change the constant value (e.g., from 3.14 to 3.14159 for PI), you need to change it only in a single location in the source code.

(3) A descriptive name for a constant makes the program easy to read.


{ 0 comments... read them below or add one }

Post a Comment