![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |
To create a constant member variable in Java use the keywordfinal
in your variable declaration. The following variable declaration defines a constant named AVOGADRO whose value is Avogadro's number (6.023 x 10^23) and cannot be changed.By convention, names of constant values are spelled in all capital letters. If your program ever tries to change a constant, the compiler will display an error message similar to the following, and refuse to compile your program.class Avo { final double AVOGADRO = 6.023e23; }AvogadroTest.java:5: Can't assign a value to a final variable: AVOGADRO
![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |