Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
What you will learn
- Declare a variable with a type, a name and a value
- Reassign a variable without repeating its type
- Apply Java's naming rules: camelCase, _ and $, no leading digits or spaces, case sensitivity
- Recognise reserved keywords and choose names that explain themselves
Before you start
- Java Hello World with Maven and the JDK
Code needs to remember things: a score, a name, a price. A variable is how it remembers. Think of a jar with a label: the label is the name, the jar holds the value. You store the value once, then use the name anywhere.
Store a value once
package org.studyeasy.s02l02;
public class Ex01_FirstVariable {
public static void main(String[] args) {
String player = "Emma";
int score = 87;
System.out.println(player + " scored " + score);
}
}
Emma scored 87
int score = 87; has three parts. int is the type (a whole number), score is the name, 87 is the value. String player works the same way for text. Inside println we only use the name, never 87 again. If the score changes, you change it in one place and every line that uses score sees the new value.
You don't need to memorise the syntax; an AI assistant can type it for you. What matters is knowing what each part means.
Reassign without the type
package org.studyeasy.s02l02;
public class Ex02_Reassign {
public static void main(String[] args) {
int score = 87;
System.out.println("Score: " + score);
score = 92;
System.out.println("Score: " + score);
}
}
Score: 87
Score: 92
Java already knows score is an int, so the second line is just score = 92;. The 87 is replaced, not added to. That's reassignment.
Write int score = 92; on that line instead and the compiler says the variable is already defined: you're trying to make a second jar with the same label.
What Java allows as a name
- camelCase is the convention: start lowercase, capitalise each new word. "user age" becomes
userAge. - A name may start with
_or$(you'll rarely need$). - A name can't start with a digit and can't contain spaces.
- Names are case sensitive:
scoreandScoreare two different variables.
package org.studyeasy.s02l02;
public class Ex03_Naming {
public static void main(String[] args) {
int userAge = 30;
int _count = 3;
int $price = 12;
int score = 87;
int Score = 10;
System.out.println("userAge: " + userAge);
System.out.println("_count: " + _count + ", $price: " + $price);
System.out.println("score: " + score + ", Score: " + Score);
}
}
userAge: 30
_count: 3, $price: 12
score: 87, Score: 10
All five are legal. score and Score hold 87 and 10 separately. Rename userAge to 2fast, or to user age with a space, and the compiler refuses both.
Some words are already taken
Keywords (reserved words) already mean something to Java, so you can't use them as your own names:
| Keyword | What it does |
|---|---|
int | a whole-number type |
class | starts a class |
if | makes a decision |
return | hands a value back from a method |
for | starts a loop |
new | creates an object |
void | a method that returns nothing |
true | a boolean value |
Name a variable class or new and the compiler stops you straight away. Pick another name.
A name that explains itself
package org.studyeasy.s02l02;
public class Ex04_GoodNames {
public static void main(String[] args) {
int d = 14;
int daysUntilLaunch = 14;
System.out.println("d = " + d);
System.out.println("Jack launches in " + daysUntilLaunch + " days");
}
}
d = 14
Jack launches in 14 days
d is valid, but 14 what? daysUntilLaunch tells you why the variable exists. Java doesn't care about the name; the next person reading your code does.
Common mistakes
int score = "87";with quotes: that's a String, and anintonly holds whole numbers, sojavacrefuses it.- Repeating the type when reassigning: "variable is already defined".
- Starting with a digit or using a space:
2fast,user age. - Mixing up case:
Scoreis notscore.
Recap
| Idea | Example |
|---|---|
| Declare: type, name, value | int score = 87; |
| Reassign: no type | score = 92; |
| camelCase names | userAge, daysUntilLaunch |
| Allowed first characters | a letter, _ or $ |
| Not allowed | a leading digit, spaces, keywords |
| Case sensitive | score and Score differ |
Next, look at what types like int really store in Java primitive types and memory.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
Open a plain project and a Maven project in IntelliJ, read an invoice app's pom.xml, follow jackson-databind from Maven Central into ~/.m2, and see why each lifecycle phase runs the ones before it.
