Skip to main content

Variables, Assignment & final

beginner15 min readLesson 6 of 180

Declaring, initializing, assigning; why final makes intent compiler-enforced.

A variable is a named box that holds a value of a known type. In Java the type is part of the declaration and never changes:

int score = 0;              // declare + initialize
score = 10;                 // assign a new value
final int maxScore = 100;   // final: cannot be reassigned

int score = 0; reads "an int named score, starting at 0". Assignment (=) means "put this value in that box" — read it as becomes, not equals.

Java distinguishes declaring (creating the box), initializing (giving it a first value), and assigning (replacing the value). A local variable that was declared but never initialized cannot be read at all:

int x;
System.out.println(x);   // compile error: variable x might not have been initialized

Use final for anything that should not change after it is set — method parameters, configuration, computed results you are about to use several times. The compiler then enforces your intent, and readers trust the value.

final String name = "Ada";
name = "Grace";   // compile error: cannot assign a value to final variable name

Next: the full type zoo.