Programmer's Picnic • Java CBSE

All Java Keywords

A simple Class 9 friendly lesson. First see the list, then read the theory and two short examples for every keyword.

Go to Keyword List

Advertisement

How to study this page

Do not try to memorize everything in one day. First learn the common keywords: class, public, static, void, main, if, else, for, while, new, return, this, and extends.

Some keywords are advanced. They are still listed because this is a complete keyword lesson.

Keyword List

Reserved keywords

Contextual keywords

Contextual keywords act like keywords only in special places. In other places, they may behave like normal names.

Theory and Examples

1. abstract reserved

Theory: Used when a class or method is incomplete. An abstract class cannot be directly converted into an object; a child class completes it.

Example 1
abstract class FeeRule {
    abstract int lateFine(int days);
}
Example 2
abstract class ReportPrinter {
    abstract void printReport();
}

2. assert reserved

Theory: Used for checking something during testing. If the condition is false, Java can stop the program with an error.

Example 1
int marks = 85;
assert marks >= 0 : "Marks cannot be negative";
Example 2
int attendance = 92;
assert attendance <= 100 : "Attendance cannot exceed 100";

3. boolean reserved

Theory: Stores only two values: true or false. Useful for yes/no situations.

Example 1
boolean feesPaid = true;
System.out.println(feesPaid);
Example 2
boolean hasLibraryCard = false;
System.out.println(hasLibraryCard);

4. break reserved

Theory: Stops a loop or exits from a switch case.

Example 1
for (int roll = 1; roll <= 50; roll++) {
    if (roll == 23) break;
}
Example 2
switch (grade) {
    case 'A': System.out.println("Excellent"); break;
}

5. byte reserved

Theory: Stores a small whole number from -128 to 127. It saves memory in large data collections.

Example 1
byte classNumber = 9;
System.out.println(classNumber);
Example 2
byte sectionCount = 4;
System.out.println(sectionCount);

6. case reserved

Theory: Used inside switch to match one possible value.

Example 1
switch (day) {
    case 1: System.out.println("Monday");
}
Example 2
switch (section) {
    case 'A': System.out.println("Room 101");
}

7. catch reserved

Theory: Handles an error after try. It prevents the program from crashing suddenly.

Example 1
try {
    int avg = total / students;
} catch (ArithmeticException e) {
    System.out.println("No students found");
}
Example 2
try {
    int n = Integer.parseInt("90");
} catch (NumberFormatException e) {
    System.out.println("Invalid number");
}

8. char reserved

Theory: Stores one character. Use single quotes.

Example 1
char grade = 'A';
System.out.println(grade);
Example 2
char section = 'B';
System.out.println(section);

9. class reserved

Theory: Creates a blueprint for objects.

Example 1
class Student {
    String name;
}
Example 2
class LibraryBook {
    String title;
}

10. const reserved but not used

Theory: Reserved by Java, but not used in normal Java programs. Use final instead.

Example 1
// Wrong in Java:
// const int MAX_MARKS = 100;
Example 2
// Correct Java:
final int MAX_MARKS = 100;

11. continue reserved

Theory: Skips the current round of a loop and moves to the next round.

Example 1
for (int roll = 1; roll <= 5; roll++) {
    if (roll == 3) continue;
    System.out.println(roll);
}
Example 2
for (int mark : marks) {
    if (mark < 0) continue;
    System.out.println(mark);
}

12. default reserved

Theory: Used in switch when no case matches. It is also used in interfaces for methods with a body.

Example 1
switch (choice) {
    default: System.out.println("Invalid choice");
}
Example 2
interface Printable {
    default void print() { System.out.println("Printing"); }
}

13. do reserved

Theory: Starts a do-while loop. The loop runs at least once.

Example 1
int attempt = 1;
do {
    System.out.println("Try " + attempt);
    attempt++;
} while (attempt <= 3);
Example 2
int page = 1;
do {
    System.out.println("Read page " + page);
    page++;
} while (page <= 5);

14. double reserved

Theory: Stores decimal numbers with good precision.

Example 1
double percentage = 87.5;
System.out.println(percentage);
Example 2
double fee = 1250.75;
System.out.println(fee);

15. else reserved

Theory: Runs when the if condition is false.

Example 1
if (marks >= 33) System.out.println("Pass");
else System.out.println("Fail");
Example 2
if (feesPaid) System.out.println("Admit card allowed");
else System.out.println("Pay fees first");

16. enum reserved

Theory: Creates a fixed set of named values.

Example 1
enum Grade { A, B, C, D }
Grade g = Grade.A;
Example 2
enum BusRoute { ROUTE_1, ROUTE_2 }
BusRoute r = BusRoute.ROUTE_1;

17. extends reserved

Theory: Used when one class inherits from another class.

Example 1
class OnlineStudent extends Student {
    String loginId;
}
Example 2
class ReferenceBook extends LibraryBook {
    String subject;
}

18. final reserved

Theory: Makes a value, method, or class unchangeable in a certain way.

Example 1
final int MAX_MARKS = 100;
Example 2
final class SchoolIdCard {
    int id;
}

19. finally reserved

Theory: Runs after try-catch, usually for cleanup work.

Example 1
try {
    System.out.println("Read marks");
} finally {
    System.out.println("Close file");
}
Example 2
try {
    int x = 10 / 2;
} catch (Exception e) {
    System.out.println("Error");
} finally {
    System.out.println("Done");
}

20. float reserved

Theory: Stores decimal numbers. Add f at the end.

Example 1
float height = 5.8f;
System.out.println(height);
Example 2
float temperature = 36.5f;
System.out.println(temperature);

21. for reserved

Theory: Repeats code a fixed number of times.

Example 1
for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}
Example 2
for (int mark : marks) {
    System.out.println(mark);
}

22. goto reserved but not used

Theory: Reserved by Java, but not used. Java avoids goto because it can make programs confusing.

Example 1
// Wrong in Java:
// goto start;
Example 2
// Use loop instead:
for (int i = 1; i <= 3; i++) {
    System.out.println(i);
}

23. if reserved

Theory: Makes a decision when a condition is true.

Example 1
if (marks >= 33) {
    System.out.println("Pass");
}
Example 2
if (attendance >= 75) {
    System.out.println("Allowed for exam");
}

24. implements reserved

Theory: Used when a class promises to follow an interface.

Example 1
interface Printable { void print(); }
class ReportCard implements Printable {
    public void print() { System.out.println("Report"); }
}
Example 2
interface Payable { void pay(); }
class FeeAccount implements Payable {
    public void pay() { System.out.println("Fee paid"); }
}

25. import reserved

Theory: Brings a class from a package into your program.

Example 1
import java.util.Scanner;
Example 2
import java.time.LocalDate;

26. instanceof reserved

Theory: Checks whether an object belongs to a class or type.

Example 1
Object obj = "Riya";
System.out.println(obj instanceof String);
Example 2
Student s = new Student();
System.out.println(s instanceof Student);

27. int reserved

Theory: Stores whole numbers.

Example 1
int marks = 95;
System.out.println(marks);
Example 2
int rollNumber = 27;
System.out.println(rollNumber);

28. interface reserved

Theory: Creates a set of methods that classes can implement.

Example 1
interface Printable {
    void print();
}
Example 2
interface LoginAllowed {
    boolean canLogin();
}

29. long reserved

Theory: Stores very large whole numbers. Add L at the end if needed.

Example 1
long schoolPhone = 919335874326L;
Example 2
long population = 1400000000L;

30. native reserved

Theory: Used for methods written outside Java, usually in C or C++. Class 9 students only need to recognize it.

Example 1
native void scanFingerprint();
Example 2
native int systemBatteryLevel();

31. new reserved

Theory: Creates a new object.

Example 1
Student s1 = new Student();
Example 2
LibraryBook book = new LibraryBook();

32. package reserved

Theory: Places a class inside a named group or folder.

Example 1
package school.records;
class Student { }
Example 2
package school.library;
class Book { }

33. private reserved

Theory: Allows access only inside the same class.

Example 1
class Student {
    private int marks;
}
Example 2
class FeeAccount {
    private double balance;
}

34. protected reserved

Theory: Allows access in the same package and child classes.

Example 1
class Person {
    protected String name;
}
Example 2
class Account {
    protected int accountNumber;
}

35. public reserved

Theory: Allows access from outside also.

Example 1
public class Main { }
Example 2
public void display() {
    System.out.println("Hello");
}

36. return reserved

Theory: Sends a value back from a method or stops a method.

Example 1
int total(int a, int b) {
    return a + b;
}
Example 2
void check(int marks) {
    if (marks < 0) return;
    System.out.println(marks);
}

37. short reserved

Theory: Stores a small whole number, larger than byte but smaller than int.

Example 1
short totalStudents = 1200;
Example 2
short libraryBooks = 3200;

38. static reserved

Theory: Belongs to the class rather than one object.

Example 1
static String schoolName = "Sunrise School";
Example 2
static void showSchool() {
    System.out.println("Sunrise School");
}

39. strictfp reserved

Theory: Makes floating-point calculations follow strict rules. It is rarely needed now.

Example 1
strictfp class Calculator {
    double area(double r) { return 3.14 * r * r; }
}
Example 2
strictfp double average(double a, double b) {
    return (a + b) / 2;
}

40. super reserved

Theory: Refers to the parent class.

Example 1
class OnlineStudent extends Student {
    OnlineStudent() { super(); }
}
Example 2
class ChildReport extends Report {
    void show() { super.show(); }
}

41. switch reserved

Theory: Selects one block from many choices.

Example 1
switch (month) {
    case 1: System.out.println("January"); break;
}
Example 2
switch (grade) {
    case 'A': System.out.println("Excellent"); break;
}

42. synchronized reserved

Theory: Controls access when many threads use the same method or data.

Example 1
synchronized void updateFee() {
    balance = balance - 500;
}
Example 2
synchronized void issueBook() {
    issuedBooks++;
}

43. this reserved

Theory: Refers to the current object.

Example 1
class Student {
    String name;
    Student(String name) { this.name = name; }
}
Example 2
class Book {
    int price;
    void setPrice(int price) { this.price = price; }
}

44. throw reserved

Theory: Manually creates an error.

Example 1
if (marks < 0) {
    throw new IllegalArgumentException("Marks cannot be negative");
}
Example 2
if (age < 5) {
    throw new IllegalArgumentException("Too young for Class 9");
}

45. throws reserved

Theory: Tells that a method may produce an error.

Example 1
void readMarks() throws Exception {
    System.out.println("Reading marks");
}
Example 2
void openFile() throws java.io.IOException {
    System.out.println("Open file");
}

46. transient reserved

Theory: Prevents a field from being saved during serialization.

Example 1
class Login {
    transient String password;
}
Example 2
class StudentSession {
    transient int otp;
}

47. try reserved

Theory: Starts a block where an error might happen.

Example 1
try {
    int avg = total / count;
} catch (Exception e) {
    System.out.println("Error");
}
Example 2
try {
    int n = Integer.parseInt("85");
} catch (Exception e) {
    System.out.println("Invalid");
}

48. void reserved

Theory: Means a method returns no value.

Example 1
void display() {
    System.out.println("Report card");
}
Example 2
public static void main(String[] args) {
    System.out.println("Start");
}

49. volatile reserved

Theory: Used with threads so that changes to a variable are visible quickly to all threads.

Example 1
volatile boolean classStarted = false;
Example 2
volatile int liveViewerCount = 0;

50. while reserved

Theory: Repeats code while a condition is true.

Example 1
int page = 1;
while (page <= 5) {
    System.out.println(page);
    page++;
}
Example 2
int balance = 1000;
while (balance > 0) {
    balance -= 100;
}

51. _ reserved identifier

Theory: The single underscore is reserved in modern Java and cannot be used as a variable name.

Example 1
// Wrong in modern Java:
// int _ = 10;
Example 2
// Correct:
int unusedMarks = 10;

52. exports contextual

Theory: Used in module-info.java to allow another module to use a package.

Example 1
module school.app {
    exports school.records;
}
Example 2
module library.app {
    exports library.books;
}

53. module contextual

Theory: Defines a Java module. This is advanced and usually not needed in Class 9.

Example 1
module school.app {
    requires java.base;
}
Example 2
module library.app {
    exports library.books;
}

54. non-sealed contextual

Theory: Used with sealed classes. It says a child class is open for further inheritance.

Example 1
non-sealed class OnlineExam extends Exam { }
Example 2
non-sealed class DigitalBook extends Book { }

55. open contextual

Theory: Used with modules to allow deep reflection for all packages.

Example 1
open module school.app {
    requires java.base;
}
Example 2
open module report.app {
    requires java.sql;
}

56. opens contextual

Theory: Used in module-info.java to open a package for reflection.

Example 1
module school.app {
    opens school.records;
}
Example 2
module library.app {
    opens library.data to report.app;
}

57. permits contextual

Theory: Used with sealed classes to list allowed child classes.

Example 1
sealed class Exam permits OnlineExam, OfflineExam { }
Example 2
sealed class Payment permits CashPayment, UpiPayment { }

58. provides contextual

Theory: Used in modules to provide a service implementation.

Example 1
module school.app {
    provides ReportService with PdfReportService;
}
Example 2
module library.app {
    provides SearchService with BookSearchService;
}

59. record contextual

Theory: Creates a simple data class with less code.

Example 1
record Student(String name, int marks) { }
Example 2
record Book(String title, int price) { }

60. requires contextual

Theory: Used in modules to say one module needs another module.

Example 1
module school.app {
    requires java.sql;
}
Example 2
module report.app {
    requires school.app;
}

61. sealed contextual

Theory: Restricts which classes can extend a class.

Example 1
sealed class Exam permits OnlineExam, OfflineExam { }
Example 2
sealed class FeePayment permits CashPayment, UpiPayment { }

62. to contextual

Theory: Used in module-info.java with exports or opens to target a specific module.

Example 1
module school.app {
    exports school.records to report.app;
}
Example 2
module library.app {
    opens library.data to admin.app;
}

63. transitive contextual

Theory: Used with requires. If A requires transitive B, users of A also read B.

Example 1
module school.app {
    requires transitive java.sql;
}
Example 2
module report.app {
    requires transitive school.app;
}

64. uses contextual

Theory: Used in modules to say that a module uses a service.

Example 1
module school.app {
    uses ReportService;
}
Example 2
module library.app {
    uses SearchService;
}

65. var contextual

Theory: Lets Java guess the local variable type from the value.

Example 1
var marks = 95;
System.out.println(marks);
Example 2
var name = "Riya";
System.out.println(name);

66. when contextual

Theory: Used in modern switch pattern matching as a condition guard. This is advanced.

Example 1
// Advanced example:
case Integer n when n >= 90 -> "Excellent";
Example 2
// Advanced example:
case String s when s.length() > 10 -> "Long name";

67. with contextual

Theory: Used in advanced module/service contexts and newer Java features. Beginners only need to recognize it.

Example 1
module school.app {
    provides ReportService with PdfReportService;
}
Example 2
module library.app {
    provides SearchService with BookSearchService;
}

68. yield contextual

Theory: Returns a value from a switch expression block.

Example 1
String result = switch (marks / 10) {
    case 10, 9 -> "A";
    default -> "Needs practice";
};
Example 2
String feeStatus = switch (paid) {
    case true -> "Paid";
    case false -> { yield "Pending"; }
};

Important note about true, false, and null

These are not counted as Java keywords. They are special literal values.

true and false
boolean present = true;
boolean absent = false;
null
String middleName = null;
System.out.println(middleName);