O'Reilly logo

Java Quick Syntax Reference by Mikael Olsson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 16

image

Constants

A variable in Java can be made into a constant by adding the final keyword before the data type. This modifier means that the variable cannot be reassigned once it has been set, and any attempts to do so will result in a compile-time error.

Local constants

A local constant must always be initialized at the same time as it is declared. The Java naming convention for constants is to use all uppercase letters and to separate the words with underscores.

final double PI = 3.14;

Constant fields

Class and instance variables can also be declared as final.

class MyClass{   final double E = 2.72;  static final double C = 3e8;}  

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required