1. Home
  2. Computing & Technology
  3. Focus on Java

constant

From Earline Gilley, for About.com

Definition: A variable with a pre-defined value. When you use constants, you do not want your program to change the value of that variable.

For example, the speed of light is a constant. It’s value never changes ( I am using 186282.397 miles-per-second in the example below), and if you want to use it in a program you should probably set it as a constant:

final double SPEED_OF_LIGHT = 186282.397;
When declaring constant variables in Java, you should set the variable name to all uppercase letters and separate words with an underscore. This pattern implies that these are constants, and it will be easier to read your code if you follow this standard naming convention.

The “final” modifier is the indicator that makes SPEED_OF_LIGHT constant and unchangeable (not to mention the actual laws of nature, as well!)

Also Known As: final variables

Explore Focus on Java

More from About.com

  1. Home
  2. Computing & Technology
  3. Focus on Java

©2008 About.com, a part of The New York Times Company.

All rights reserved.