Rules for Naming Variables in Java
Java follows strict rules defined by the language specification.
A variable name must begin with
❌ Cannot:
int 1age; // ❌ Invalid
int student age; // ❌ Invalid
int class; // ❌ Invalid (class is a keyword)
Java is case-sensitive, so:
These are two different variables.
Java Naming Conventions (Best Practices)
These are not rules (your code will still run), but they are industry standards.
Use camelCase for Variables
Start with a lowercase letter. Capitalize each new word.
Use Meaningful Names
Good:
Bad:
Names should describe the purpose of the variable.
Boolean Variables
Often start with:
boolean isActive;
boolean hasLicense;
boolean canVote;
Constants (Special Case)
Constants use:
final double PI = 3.14159;
final int MAX_STUDENTS = 50;
Summary Table
Type |
Convention |
Example |
Normal variable |
camelCase |
studentName |
Boolean variable |
camelCase with is/has |
isPassed |
Constant |
UPPER_CASE |
MAX_SIZE |
Why Naming Conventions Matter