Keywords and Identifiers in Java: A Simple Beginner’s Guide

  • Last Updated: July 18, 2026
  • By: javahandson
  • Series
img

Keywords and Identifiers in Java: A Simple Beginner’s Guide

Learn keywords and identifiers in Java the easy way. See the full keyword list, naming rules, camelCase vs PascalCase conventions, and common beginner mistakes.

1. Introduction

When you write your first Java program, you name a lot of things. You name variables, methods, and classes. Some words you pick yourself. Others are already taken by the language.

Keywords and identifiers in Java sit right at the center of this. A keyword is a word Java has reserved for itself. An identifier is a name you create for your own stuff.

Mix these two up, and the compiler complains fast. So it helps to know which words belong to Java and which ones you get to choose.

In this guide, we start with keywords. Then we move on to identifiers and the rules for naming them. Along the way, you’ll see plenty of tiny examples.

Here’s what we’ll walk through:

  • What a keyword is, and why you can’t use one as a name
  • The full list of Java keywords, grouped so they’re easier to learn
  • What an identifier is, and the rules that make a name valid
  • Naming conventions that pro Java developers follow
  • Common mistakes beginners make, plus interview questions

You don’t need any deep Java background. If you’ve written even one small program, you’re ready to follow along.

One quick note before we start. This topic feels tiny, but it shapes everything you write later. Clean names and correct keyword use save you hours of confusing errors down the road.

Keywords and Identifiers in Java

2. What Is a Keyword?

A keyword is a word that Java already owns. The language uses it for a special job, so you can’t grab it for your own names.

Think of words like class, if, and return. Each one tells the compiler to do something specific. You simply can’t rename them or reuse them.

2.1 Keywords Are Reserved

Every keyword is reserved. Java holds onto it for good. You can’t use it as a variable, method, or class name.

Try to name a variable class, and the compiler stops you cold. It reads class as a signal to start a class definition, not as a name.

int class = 5;   // ERROR: 'class' is a keyword
int number = 5;  // OK: 'number' is a normal name

The first line fails right away. The second line works fine, because number is a name you’re free to pick.

Why does Java lock these words down? Each keyword triggers a fixed action in the compiler. If you could rename them, the compiler would get confused. It couldn’t tell your name apart from its own command. So it keeps them separate.

2.2 Keywords Are Always Lowercase

Here’s a handy detail. Every Java keyword is written in lowercase. There’s no keyword with a capital letter in it.

So if, else, and while are keywords. But If, Else, and While are not. Java treats them as ordinary names you could actually use.

This is why Java is case-sensitive. The word public is a keyword. The word Public is just a plain identifier, and Java sees them as two different things.

2.3 A Few Special Words That Look Like Keywords

Some words feel like keywords but aren’t, strictly speaking. Java calls them literals. The values true, false, and null fall into this group.

You can’t use these three as names either. Still, they aren’t real keywords. They stand for fixed values instead of language commands.

For everyday coding, you can treat them like reserved words. Just remember you can’t name a variable true or null.

3. The List of Java Keywords

Java has around 50 keywords. That sounds like a lot to memorize. Good news: you won’t cram them all at once.

Most of these you’ll pick up naturally as you code. Below, we group them by their job so they’re easier to make sense of.

3.1 Keywords for Data Types

These describe the primitive types that hold basic values. You’ll type them constantly, so they’ll stick fast.

  • byte, short, int, long — whole numbers of different sizes
  • float, double — numbers with decimals
  • char — a single character
  • boolean — either true or false

You already met most of these on day one. int and boolean show up in almost every program.

There’s also the void keyword, which sits near this group. It isn’t a data type, but it goes where a type would. You write it when a method returns nothing at all.

3.2 Keywords for Control Flow

These steer the path your program takes. They decide what runs, what repeats, and what gets skipped.

  • if, else — make a choice
  • switch, case, default — pick from many options
  • for, while, do — repeat a block
  • break, continue — jump out of or skip a loop
  • return — hand a value back from a method

Loops and conditions lean on these words heavily. You’ll use if and for more than almost anything else.

3.3 Keywords for Classes and Objects

These build the object-oriented side of Java. They shape your classes and how they relate to each other.

  • class, interface, enum — define types
  • extends, implements — set up inheritance
  • new — make an object
  • this, super — refer to the current or parent object
  • abstract, final — control how types behave

Don’t stress about all of these now. You’ll grow into them as you learn about objects and inheritance.

3.4 Keywords for Access and Modifiers

These set who can see your code and how it works. They act like labels stuck onto your fields and methods.

  • public, private, protected — control visibility
  • static — belongs to the class, not an object
  • final — can’t be changed once set
  • synchronized, volatile, transient — used in advanced cases

The first three come up early. The advanced ones wait until you touch threads or serialization.

3.5 Keywords for Handling Errors

These deal with things going wrong. When your code hits a problem, these words help you catch it and recover.

  • try — mark code that might fail
  • catch — handle the failure
  • finally — run cleanup either way
  • throw, throws — send and declare an error

You’ll meet these once you start reading files or parsing input. For now, just know they group together around errors.

3.6 Keywords for Packages

Two small words help you organize code across files. They come up in almost every real project.

  • package — declare which folder a file belongs to
  • import — pull in code from another package

You’ve probably typed import already without thinking about it. That line at the top of a file uses this exact keyword.

3.7 The Full Reference Table

Here’s the whole set in one place. Bookmark it, but please don’t try to memorize it in one sitting.

Group Keywords
Data types byte, short, int, long, float, double, char, boolean
Control flow if, else, switch, case, default, for, while, do, break, continue, return
Classes/objects class, interface, enum, extends, implements, new, this, super, abstract, final, instanceof
Access/modifiers public, private, protected, static, final, synchronized, volatile, transient, native, strictfp
Exceptions try, catch, finally, throw, throws
Package/import package, import
Others void, assert, const*, goto*

Notice the stars on const and goto. Java reserves both words but never actually uses them. They’re kept off-limits so nobody names a variable with them.

3.7.1 The Odd Ones: const and goto

These two are called reserved but unused. Java holds them back yet gives them no job. You still can’t use either as a name.

Why keep them? The designers wanted room to add features later without breaking old code. So far, they never have.

4. What Is an Identifier?

An identifier is a name you invent. You use it to label a variable, a method, a class, or a package. It’s how you refer to things in your code.

Take the line int score = 10;. Here, score is an identifier. You made it up, and now it points to the value 10.

4.1 Identifiers Name Your Stuff

Every custom name in your program is an identifier. Variable names, method names, class names — all of them count.

int age = 25;              // 'age' is an identifier
String firstName = "Sam";   // 'firstName' is an identifier
void printReport() { }      // 'printReport' is an identifier

See how each name describes what it holds or does? Good identifiers read almost like plain English.

4.2 Why Names Matter

A clear name saves you time later. Read x = x * 2 and you’ll wonder what x means. Read price = price * 2 and it clicks instantly.

So pick names that explain themselves. Future you, reading this code in six months, will be grateful.

4.3 Where Identifiers Show Up

Identifiers appear all over your programs. Once you start looking, you’ll see them everywhere.

  • Variable names, like total or userAge
  • Method names, like save or getPrice
  • Class names, like Student or Invoice
  • Package names, like com.myapp.model
  • Interface and enum names too

Every one of these is a name you or another coder created. None of them are keywords. That’s the line that splits the two groups.

5. Rules for Valid Identifiers

Java has firm rules about identifier names. Break one, and your code won’t compile. Let’s go through them one by one.

5.1 Allowed Characters

An identifier can contain letters, digits, the dollar sign, and the underscore. That’s the full set of legal characters.

  • Letters: a to z and A to Z
  • Digits: 0 to 9
  • The underscore: _
  • The dollar sign: $

Most of the time you’ll stick to letters. The dollar sign is usually left for tools that generate code, so skip it in your own work.

5.2 The First Character Rule

Here’s a big one. A name can’t start with a digit. It has to begin with a letter, an underscore, or a dollar sign.

int total5 = 100;   // OK: digit is not first
int _count = 5;     // OK: underscore first
int 5total = 100;   // ERROR: can't start with a digit

The last line fails because it opens with 5. Move the digit anywhere else, and the name becomes valid.

5.3 No Spaces Allowed

You can’t put a space inside an identifier. Java would read a space as the end of one name and the start of something else.

So first name with a space is invalid. To join words, glue them together and capitalize, like firstName. We’ll cover that style soon.

5.4 No Keywords as Names

We said this before, but it’s worth repeating. You can’t use any keyword as an identifier. Words like class, new, and return are off-limits.

The compiler already gave those words a meaning. Trying to reuse one just confuses it, and you get an error.

5.5 Case Sensitivity

Java cares about upper and lower case. The names age, Age, and AGE are three separate identifiers, not one.

int age = 20;
int Age = 30;
System.out.println(age);  // prints 20
System.out.println(Age);  // prints 30

Both variables live side by side without a clash. Handy, sure, but it also causes bugs when you mistype a name. Watch your capitals.

5.6 A Quick Valid vs Invalid Table

Let’s put the rules together. Here are some names side by side, valid next to invalid.

Identifier Valid? Reason
userName Yes Letters only, clean and clear
_temp Yes Underscore start is allowed
count1 Yes Digit is not first
total$ Yes Dollar sign is legal
1stPlace No Starts with a digit
first name No Contains a space
class No It’s a reserved keyword
my-var No Hyphen is not allowed

Run down that list a couple of times. Once the pattern sticks, you’ll spot bad names at a glance.

6. Naming Conventions

Rules tell you what Java allows. Conventions tell you what good developers actually do. They aren’t enforced by the compiler, yet everyone follows them.

Why bother? Because shared conventions make code easy to read across teams. Your names look like everyone else’s, so nobody has to guess.

6.1 camelCase for Variables and Methods

For variables and methods, use camelCase. Start with a lowercase letter. Then capitalize the first letter of each new word.

int itemCount = 0;
String userEmail = "sam@mail.com";
void calculateTotal() { }

The name reads like a little hump-backed word. Each capital marks where a new word begins, which keeps things readable.

6.2 PascalCase for Classes

For class names, use PascalCase. It’s like camelCase, but the very first letter is capital too.

class BankAccount { }
class StudentRecord { }
class OrderProcessor { }

This little difference helps a lot. A capital first letter tells you it’s a class. A lowercase one tells you it’s a variable or method.

6.3 UPPER_CASE for Constants

For constants, use all capitals with underscores between words. A constant is a value that never changes, marked with static final.

static final int MAX_USERS = 100;
static final double PI = 3.14159;
static final String APP_NAME = "MyApp";

The shouting capitals send a clear signal. When you see MAX_USERS, you know right away it’s a fixed value you shouldn’t reassign.

6.4 Meaningful Names Beat Short Names

Short names save typing but cost you clarity. A name like n tells you nothing. A name like numberOfStudents tells the whole story.

  • Avoid single letters, except for simple loop counters like i or j
  • Skip vague names like data, temp, or thing
  • Spell words out; don’t write usrNm when userName reads better

A slightly longer name is a small price. The clarity you gain pays off every time someone reads the code, including you.

6.5 Boolean Names Read Best as Questions

For a true-or-false variable, name it like a question. Words like isReady, hasAccess, or canEdit make the intent obvious.

boolean isActive = true;
boolean hasPermission = false;
boolean canDelete = true;

Read one aloud, and it sounds like a yes-or-no question. That tiny habit makes your if-conditions much easier to follow.

6.6 Convention Cheat Sheet

Here’s the whole naming style in one small table. Keep it nearby until it becomes second nature.

What you’re naming Style Example
Variable camelCase totalPrice
Method camelCase getUserName()
Class PascalCase CustomerOrder
Interface PascalCase Runnable
Constant UPPER_CASE MAX_SIZE
Package lowercase com.myapp.utils

Follow these, and your code fits right in with the wider Java world. That’s the whole point of a convention.

7. A Practical Example

Let’s tie it all together in one small program. We’ll build a tiny class that tracks a bank account.

7.1 The Full Class

Here’s the code. Read it once, then we’ll pull apart the naming choices below.

class BankAccount {
    static final double MIN_BALANCE = 100.0;  // constant
    private double balance;                    // variable
 
    void deposit(double amount) {              // method
        balance = balance + amount;
    }
 
    double getBalance() {
        return balance;
    }
}

7.2 Spotting Each Style

Look at the names, and you’ll see every convention at work:

  • BankAccount uses PascalCase, so we know it’s a class
  • MIN_BALANCE uses UPPER_CASE, so we know it’s a constant
  • balance and amount use camelCase, so they’re variables
  • deposit and getBalance use camelCase, so they’re methods

Every name here follows a keyword rule and a convention. None of them clash with reserved words, and each one reads clearly.

7.3 Why This Matters

A stranger could read this class and follow it fast. The names carry meaning, and the styles hint at each thing’s role.

That’s the payoff of good keyword sense and clean identifiers. Your code explains itself, with far fewer comments needed.

7.4 Trying It Yourself

Want to lock this in? Try renaming a few things and watch what breaks.

  • Change balance to double, and the code won’t compile — double is a keyword
  • Change MIN_BALANCE to camelCase, and it still runs, but the constant convention slips
  • Add a variable named new, and the compiler rejects it right away

Playing with small breaks like these teaches the rules fast. The compiler becomes your quick, honest tutor.

8. Common Mistakes and Pitfalls

A handful of slip-ups trip up almost every beginner. Let’s name them so you can steer clear.

8.1 Using a Keyword as a Name

This is the classic one. You reach for a natural word like class or new, and the compiler rejects it. Pick a different name instead, like className or newItem.

8.2 Starting a Name with a Digit

Names like 2ndPlace or 100days fail every time. Flip them around to placeTwo or days100, and they compile cleanly.

8.3 Forgetting Case Sensitivity

You write userName in one spot and username in another. Java sees two different names, and your value seems to vanish. Keep your capitals consistent.

8.4 Adding Spaces or Special Characters

A name like user name or user-name won’t work. Only letters, digits, underscore, and dollar sign are allowed. Join words with camelCase instead.

8.5 Vague or Cryptic Names

Names like a, tmp, or x1 compile fine but confuse everyone later. They don’t break the build, yet they break readability. Spell out what the value really is.

8.6 Mixing Up Conventions

Sometimes you’ll name a class in camelCase or a constant in lowercase. The code still runs, so it feels harmless. Over a big project, though, the mess adds up.

Stick to one style per kind of name. PascalCase for classes, camelCase for variables, UPPER_CASE for constants. That steady habit keeps a whole codebase readable.

9. Interview Questions

Q: What is the difference between a keyword and an identifier in Java?

A: A keyword is a word Java reserves for itself, like class or return, and you can’t use it as a name. An identifier is a name you create for your own variables, methods, and classes, like score or userName.

Q: How many keywords does Java have?

A: Java has around 50 keywords. Two of them, const and goto, are reserved but never actually used by the language.

Q: Can an identifier start with a number in Java?

A: No. An identifier must start with a letter, an underscore, or a dollar sign. A digit can appear anywhere except as the first character, so total5 is fine but 5total fails.

Q: Are true, false, and null keywords in Java?

A: Not technically. They are literals, not keywords. Even so, you can’t use them as identifiers, so you can treat them like reserved words in everyday coding.

Q: Is Java case-sensitive for identifiers?

A: Yes. Java treats age, Age, and AGE as three separate names. Keeping your capitalization consistent avoids bugs where a value seems to disappear.

Q: What naming conventions should I follow in Java?

A: Use camelCase for variables and methods, PascalCase for class names, and UPPER_CASE with underscores for constants. Packages are written in lowercase. These aren’t enforced by the compiler, but every Java developer follows them.

Q: Can I use a dollar sign or underscore in a Java identifier?

A: Yes, both are legal. The underscore is common in constant names, while the dollar sign is usually left for tool-generated code, so it’s best to avoid it in code you write by hand.

Q: Why can’t I name a variable class or new in Java?

A: Because class and new are keywords with a fixed job in the compiler. Reusing one confuses the compiler, since it can’t tell your name apart from its own command, so it throws an error.

10. Conclusion

Let’s recap the main ideas. A keyword is a reserved word that Java owns, so you can never use it as a name.

An identifier is a name you create for your variables, methods, and classes. It must start with a letter, underscore, or dollar sign, and it can’t contain spaces.

On top of the rules, conventions guide good naming. Use camelCase for variables and methods, PascalCase for classes, and UPPER_CASE for constants.

One more thing worth holding onto. Java is case-sensitive, so age and Age are two different names. Keep your capitals steady, and you’ll dodge a whole class of silly bugs.

Get these habits early, and the rest of Java feels smoother. Clean names and correct keyword use are the quiet foundation of readable code.

Further Reading

Leave a Comment