Java Keywords & Identifiers
This guide explains what keywords and identifiers are in Java, their rules, best practices, and examples. It’s beginner-friendly, interview-ready, and optimized for Google ranking.
What Are Keywords in Java?
- Keywords are reserved words in Java that have a special meaning in the language.
- You cannot use keywords as names for variables, classes, methods, or identifiers.
- Java has 52 keywords (depending on version) and 5 reserved literals like
true,false,null.
List of Java Keywords (Java 17)
| Keyword | Keyword | Keyword | Keyword |
|---|---|---|---|
| abstract | assert | boolean | break |
| byte | case | catch | char |
| class | const* | continue | default |
| do | double | else | enum |
| extends | final | finally | float |
| for | goto* | if | implements |
| import | instanceof | int | interface |
| long | native | new | package |
| private | protected | public | return |
| short | static | strictfp | super |
| switch | synchronized | this | throw |
| throws | transient | try | void |
| volatile | while | yield | record |
| sealed | permits | non-sealed | var |
| module | open | requires | exports |
⚠️ const and goto are reserved but not used in Java.
What Are Identifiers in Java?
- Identifiers are names you give to classes, methods, variables, and other elements in Java.
- They help uniquely identify these program elements in your code.
Rules for Identifiers
- Must start with a letter,
$, or_. - Can contain letters, digits,
$, or_. - Cannot be a keyword.
- Java is case-sensitive (
age≠Age). - No spaces or special characters allowed.
Valid Identifiers
int age;
String studentName;
double _salary;
int $count;
Invalid Identifiers
int 2age; // starts with a digit
String student-name; // contains hyphen
int class; // keyword
Naming Conventions (Best Practices)
| Element | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentDetails |
| Method | camelCase | calculateSalary() |
| Variable | camelCase | totalSalary |
| Constant | UPPERCASE | MAX_AGE |
| Package | lowercase | com.example.project |
Following conventions improves readability, collaboration, and maintainability.
Example Program Using Keywords & Identifiers
public class Student {
// Instance Variables (Identifiers)
String studentName;
int age;
final double PI = 3.14159; // Constant
// Method
public void displayInfo() {
System.out.println("Name: " + studentName);
System.out.println("Age: " + age);
System.out.println("PI Value: " + PI);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.studentName = "John";
s1.age = 25;
s1.displayInfo();
}
}
Output:
Name: John
Age: 25
PI Value: 3.14159
🏆 Top 5 Java Interview Questions - Java Keywords & Identifiers
1. What is the difference between a keyword and an identifier?
Answer:
| Feature | Keyword | Identifier |
|---|---|---|
| Definition | Reserved word in Java with a special meaning | Name given to variables, methods, classes, etc. |
| Usage | Cannot be used as names | Can be used to name user-defined elements |
| Example | class, int, if, for | myVar, calculateTotal, Student |
| Modifiable | No | Yes |
2. How many keywords are there in Java 17?
Answer:
- Java 17 has 51 reserved keywords, including
sealed,non-sealed, andrecord. - Example keywords:
abstract,assert,boolean,break,case,class,continue,enum,final,for,if,import,instanceof,new,switch,while,record,sealed.
3. Can an identifier start with a number? Why or why not?
Answer:
- No, an identifier cannot start with a digit.
- Reason: Java’s syntax rules require identifiers to start with a letter (A–Z, a–z), underscore (_) or dollar sign ($).
- Example of invalid identifiers:
1var,2num - Example of valid identifiers:
var1,_count,$value
4. Explain the difference between final, static, and regular variables.
Answer:
| Type | Description | Example |
|---|---|---|
| Regular Variable | Standard variable tied to an object (instance) or method (local) | int age = 25; |
| Static Variable | Shared by all objects of a class; belongs to class, not instance | static int count = 0; |
| Final Variable | Constant; value cannot be changed after initialization | final double PI = 3.14159; |
Notes: A variable can be both static and final (constant shared by all instances).
5. Is class a valid identifier? Why or why not?
Answer:
- No,
classis not a valid identifier. - Reason: It is a reserved keyword in Java used to define classes, and keywords cannot be used as names for variables, methods, or classes.
