Skip to Content
Java Quick Syntax Reference
book

Java Quick Syntax Reference

by Mikael Olsson
January 2018
Intermediate to advanced content levelIntermediate to advanced
115 pages
1h 37m
English
Apress
Content preview from Java Quick Syntax Reference
© Mikael Olsson 2018
Mikael OlssonJava Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-3441-9_16

16. Constants

Mikael Olsson1 
(1)
Hammarland, Länsi-Suomi, Finland
 

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 can’t be reassigned once it’s 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’s declared. The Java naming convention for constants is to use all uppercase letters and to separate 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; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Core Java 11 Advanced, Second Edition

Core Java 11 Advanced, Second Edition

Cay S. Horstmann
Java Illuminated, 4th Edition

Java Illuminated, 4th Edition

Julie Anderson, Hervé J. Franceschi

Publisher Resources

ISBN: 9781484234419Purchase LinkPublisher Website