Clean β’ Professional
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.
true, false, null.| 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.
$, or _.$, or _.age β Age).| Valid Identifiers | Invalid Identifiers |
|---|---|
int age; | int 2age; // starts with digit |
String studentName; | String student-name; // invalid char |
double _salary; | int class; // keyword |
| 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.
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