Java Variables & Declaration
A variable is a named storage container in memory. Because Java is statically typed, you must specify the exact data type before the variable name. Once declared, Java guarantees that container will only hold values matching that type.
"Variables are like labeled storage bins in a workshop. A bin labeled "Screws" (type int) won't let you shove a pair of shoes (String) into it."
Deep Dive: How It Works
Declaration syntax: `type variableName = initialValue;`
Java variables must be declared before they can be used or referenced.
You can declare a variable first and assign its value later, or declare and initialize in one line.
The `final` keyword marks a variable as a constant (its value cannot be modified after assignment).
Syntax Blueprint
dataType variableName = value; final dataType CONSTANT_NAME = value;
Specify the type (e.g. int, double, String), give it a camelCase name, and assign with =.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Re-declaring type on update (int score = 10; int score = 20;)Why it happens: Specifying the type twice in the same scope causes "variable score is already defined".
How to fix: Drop the type keyword on reassignment: score = 20;
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Declare a student score variable
Declare an int variable named "score" with the value 95.
Print "Student Score: " + score.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.