bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Tutorial
Java•Java Tutorial

Java Constants (final)

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Constants (final)?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ int myNum = 15;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

When you do not want a variable's value to change, use the final keyword.
When to Use final?
Constants (final keyword)

Constants (final keyword)

When you do not want a variable's value to change, use the final keyword.

A variable declared with final becomes a constant , which means unchangeable and read-only:

final int myNum = 15;
myNum = 20; // Error: cannot assign a value to final variable 'myNum'

When to Use final?

You should declare variables as final when their values should never change. For example, the number of minutes in an hour, or your birth year:

final int MINUTES_PER_HOUR = 60;
final int BIRTHYEAR = 1980;

Note

By convention, final variables in Java are usually written in upper case (e.g. BIRTHYEAR ). It is not required, but useful for code readability and common for many programmers.

Previous

Java Identifiers

Next

Java Data Types