home / java / java-syntax-rules-you-cant-break-keywords-that-must-appear-immediately
Java

Java Syntax Rules You Can’t Break: Keywords That Must Appear Immediately

Java is known for its strict syntax. While beginners usually focus on learning loops, conditions, and object-oriented programming, many compilation errors happen simply because certain keywords are not placed where Java expects them to be.

In this article, we’ll explore the most common Java syntax rules where the order of statements matters.


1. else Must Immediately Follow an if

One of the most common mistakes is placing another statement between an if block and its corresponding else.

✅ Correct

if (age >= 18) {
    System.out.println("Eligible to vote");
}
else {
    System.out.println("Not eligible");
}

❌ Incorrect

if (age >= 18) {
    System.out.println("Eligible to vote");
}

System.out.println("Checking eligibility...");

else {
    System.out.println("Not eligible");
}

Why?

The else keyword belongs directly to the preceding if. Once another statement appears, Java considers the if statement complete.


2. catch Must Come Immediately After try

A catch block cannot be separated from its try block.

✅ Correct

try {
    int result = 10 / 0;
}
catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
}

❌ Incorrect

try {
    int result = 10 / 0;
}

System.out.println("Completed");

catch (ArithmeticException e) {
    System.out.println("Error");
}

Why?

A catch block is directly associated with its try block. Any interruption breaks the structure.


3. while Must Follow a do Block

Unlike a normal while loop, a do-while loop ends with the while statement.

✅ Correct

do {
    System.out.println("Hello");
} while (count < 5);

❌ Incorrect

do {
    System.out.println("Hello");
}

System.out.println("Done");

while (count < 5);

Why?

The while statement completes the do-while loop syntax.


4. super() or this() Must Be the First Statement in a Constructor

When calling another constructor or a parent constructor, Java requires it to be the very first statement.

✅ Correct

class Employee {

    Employee() {
        System.out.println("Employee created");
    }
}

class Developer extends Employee {

    Developer() {
        super();
        System.out.println("Developer created");
    }
}

❌ Incorrect

Developer() {
    System.out.println("Creating developer...");
    super();
}

Why?

Java initializes the parent object before executing any other constructor logic.


5. Multiple catch Blocks Must Be Consecutive

If you have multiple exception handlers, they must appear one after another.

✅ Correct

try {
    // code
}
catch (ArithmeticException e) {

}
catch (Exception e) {

}
finally {

}

❌ Incorrect

try {

}

System.out.println("Hello");

catch (Exception e) {

}

6. finally Must Follow catch (or try)

The finally block is always attached to a try statement.

✅ Correct

try {
    // code
}
catch (Exception e) {

}
finally {
    System.out.println("Cleanup");
}

Why Does Java Enforce These Rules?

Java’s compiler follows a strict grammar. Certain keywords are not standalone statements—they are continuations of previous constructs.

Think of them like puzzle pieces:

  • ifelse
  • trycatch
  • dowhile
  • Constructor → super() / this()

If another statement appears in between, Java no longer knows which construct the keyword belongs to, resulting in a compilation error.


Tips to Avoid These Mistakes

  • Keep related blocks together.
  • Use proper code formatting and indentation.
  • Let your IDE auto-format your code.
  • Read compiler error messages carefully—they often point to the exact location of the issue.

Final Thoughts

Java is a strongly structured language, and syntax order is just as important as syntax itself. Understanding these “must come next” rules will help you avoid frustrating compilation errors and write cleaner, more maintainable code.

As you continue learning Java, remember this simple rule:

In Java, it’s not just what you write—it’s also where you write it.