Named Constants in Java


A named constant is an identifier that represents a permanent value.

The value of a variable may change during the execution of a program, but a named constant, or simply constant, represents permanent data that never changes.

Here is the syntax for declaring a constant:

A constant must be declared and initialized in the same statement. The word final is a Java keyword for declaring a constant.

For example, you can declare PI as a constant .

There are three benefits of using constants:

  1. You don’t have to repeatedly type the same value if it is used multiple times;
  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; and
  3. A descriptive name for a constant makes the program easy to read.

Would You Mind Sharing?


Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.