Java Notes
Identifiers are the names you choose in Java, such as a variable name, class name, or method name. Clear names tell a reader what the code is storing or doing.
An identifier is simply a name that you choose in a Java program. When you name a variable studentName, you have created an identifier. When you name a class StudentProfile, that is also an identifier.
Names matter because Java needs to know what you mean, and another person should be able to read your code without guessing.
See identifiers in a real program
Before reading the code, know what the program is trying to do: it stores one student's name and how many lessons that student completed. Then it prints a sentence about that student.
For Java to do this, every important thing needs a name. The names are identifiers. They work like labels on boxes: one label tells Java where the name is stored, another label tells Java what action to perform.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read the code in this order:
StudentProfilenames the complete type of thing we are describing: a student's profile.studentNameis the label for the text"Asha". Later, Java can use that label to get the student's name.completedLessonsis the label for the number12.showProgressnames a group of instructions. Its name says what the instructions do.profileis a short label for the one StudentProfile created inmain. We use it inprofile.showProgress()to say: “this profile, show your progress.”
Notice the difference between the names you choose and Java's built-in words. You chose studentName, showProgress, and profile. Java already reserves words such as class, public, int, and static, so you cannot use those reserved words as your own labels.
Rules for a valid name
You cannot choose every possible name. Java reads symbols in a specific way, so it has rules that let it recognise where a name begins and ends. If a name breaks one of these rules, Java shows an error before the program starts.
For example, Java reads 2user as a number 2 followed by a word, not as one name. It reads first-name as first minus name, because - means subtraction. That is why these names are not allowed.
| Rule | Good name | Not allowed | Why |
|---|---|---|---|
Start with a letter, _, or $ | total, user2 | 2user | A digit cannot be the first character. |
Use letters, digits, _, or $ after that | chapter2 | chapter-2 | A hyphen means subtraction to Java. |
| Do not use spaces | firstName | first name | Java sees a space as the end of a word. |
| Do not use Java's reserved words | className | class | Java already uses class for its own rule. |
| Match capital letters exactly | score and Score are different | — | Java is case-sensitive. |
Try to create a name by joining meaningful words without spaces: firstName, coursePrice, totalMarks, and chapter2 are all easy for Java and people to read.
_count and $price can compile, but clear letter-based names such as itemCount and coursePrice are easier to read. Also remember that Java sees score and Score as two separate names. Changing only one capital letter can therefore create a confusing error.
Write names that explain themselves
Code is read many more times than it is written. A good identifier lets someone understand the line without asking, “What does this number mean?”
Compare these two ways to store the same information:
| Hard to understand | Clear meaning |
|---|---|
int d = 30; | int daysInMonth = 30; |
double p = 499.0; | double coursePrice = 499.0; |
boolean b = true; | boolean isPublished = true; |
The first column is not invalid Java; it will run. The problem is that d, p, and b do not explain their purpose. After a few days, even the person who wrote the code may forget what they meant.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
In the program, daysInMonth tells the reader exactly what 30 means. coursePrice tells us what the decimal number represents. isPublished makes the true/false question clear: “Is the course published?”
Avoid names such as d, p, or x1 when they represent real information. For a true/false value, start with words such as is, has, or can: isLoggedIn, hasAccess, or canEdit. Short names are okay only when the meaning is obvious in a tiny piece of code, such as index inside a short loop.
Naming style that Java developers use
These styles are not compiler rules; Java will still run a name with different capital letters. They are shared habits that make code look familiar and consistent.
| What you are naming | Normal style | Example |
|---|---|---|
| Class | Start each word with a capital letter | BankAccount, StudentProfile |
| Variable or method | Start lower-case; capitalise later words | totalMarks, calculateTotal() |
| Constant value that should not change | Use capital letters and _ between words | MAX_ATTEMPTS |
| Package | Lower-case words | school.app |
Try it yourself
Make a program called BookDetails. Create variables for a book title, page count, and whether the book is available. Choose clear identifier names, print them, then change one name to an unclear name and notice how much harder the code becomes to read.
Remember this
- An identifier is a name you choose for something in Java.
- A valid name must follow Java's character rules.
- A clear name explains the value or action without requiring a comment.
totalandTotalare different names in Java.
Continue the lesson path