Ad space (banner)
โ˜•Java Lessons
Lesson 19 / 68

Constants (final)

This lesson covers declaring constants with final in Java, so you can safely handle values that shouldn't change. It's for anyone searching "Java final constant usage."

The final keyword creates a constant whose value can't be reassigned afterward.

The example defines a perfect score as a constant with final int MAX_SCORE = 100, and uses it in building a message. Writing a constant's name in uppercase with underscores, like MAX_SCORE, is a convention shared across many languages.

A common early mistake is trying to reassign a variable marked final, which produces a compile error. Since final-declared values are caught as an error at compile time, this prevents the mistake of accidentally overwriting them.

In real projects, constants are a basic technique used constantly to avoid magic numbers and communicate a value's intent through its name.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)