Tutorial:

The most common compile time errors in Java

By Cameron McKenzie

TheServerSide.com

It's always a challenge to learn a new programming language, as simple mistakes can generate errors that are both cryptic and baffling for the uninitiated. And if you’re not lucky enough to have an experienced programmer looking over your shoulder and providing guidance, troubleshooting your code can be extremely frustrating.

If you’re learning Java and you’re having issues compiling and running your code, your problems will fall into one of two categories: either you have encountered a compile time error, which means your program failed to compile, or you have run into a runtime error, which occurs when your program successfully compiles but fails to run without generating an error. Of course, before you can ever encounter a runtime error, your code must first be able to compile, so here we’re going to look at the most common reasons why developers might encounter a compile time error.

The most common compile time errors

We'll use the following code as a sample Java class for discussion:

public class Game {
  
  public static void main(String args[]) {

    System.out.println("If I choose Paper,");
    System.out.println("And you choose Scissors,");
    System.out.println("Then I win, and you lose!");

  }

}

Incorrect naming of the Java file The name of the Java file in which  you write you’re  Java code must exactly match the name of the public class contained within the associated Java file. So, if your code contains a public class named Game, the associated Java file must be named Game.java, not game.java, GAME.java or MyGame.java. The name of the file, and the name of the public class within that file, must match exactly, both in spelling and in the casing of the letters.

Improper casing of code – Java is case sensitive, so public is different from Public which is different from puBliC. Java newbies tend to capitalize letters that should be lower case, and vice-versa, and as a result of improper casing, the code they write fails to compile. To further complicate this problem, the compile time error messages that come about due to capitalization problems are often cryptic and unhelpful.  For example, if you capitalized the first letter in the words public static and void in the main method declaration, you would get the following cryptic error message, indicating that a semi-colon is expected, which really isn’t the problem at all:

C:\_jdk1.7\bin>javac Game.java

Game.java:3: error: ';' expected

  Public Static Void main (String args[]) {

               ^

1 error


Mis-matched bracketsFor every open bracket you see in your code, be it a square bracket, curly bracket or round bracket, you need a matching and corresponding closed bracket. Sometimes, a programmer will forget the closing bracket for a method, or they will remember to put in a closing bracket for a method but forget to close the class. Regardless of why it happens, if your brackets don’t all match up, you’ll end up getting a compile time error.

For example, by leaving out the last curly bracket that closes off the Java class, attempting to compile the code would generate the following compile time error:

C:\_jdk1.7\bin>javac Game.java

Game.java:11: error: reached end of file while parsing

}

 ^

1 error

Personally, whenever I create a new method or class, immediately after typing in the opening bracket, I always throw in a few carriage-returns and then add a closing bracket. Only after I have matched my brackets will I start coding the body of the class or method. That way, your brackets always match, and you can code your class and methods without having to worry about putting in the closing brace at some point in the future.

Omitted semi-colons – The other common coding mistake that people getting familiar with the Java language make is the omission of the required semi-colons. As a rule, every statement in Java ends with a semi-colon. Sadly though, that rule can sometimes be as confusing as it is helpful, especially if you have a hard time figuring out what is and what isn’t a statement.

For example, all of the System.out.print invocations in the body of a method end with semi-colons. If we were to leave a semi-colon off a System.out statement in the method body, we’d end up with a compile time error message that looks like this:

C:\_jdk1.7\bin>javac Game.java

Game.java:7: error: ';' expected

    System.out.println("Then I win and you lose!")

                                                  ^

1 error

The confusing part is due to the fact that not every line of code you write in Java is a statement. For example, the class declaration is not considered a statement, so it is not followed by a semi-colon. Similarly, a method declaration is not considered a statement, so it is not followed by a semi-colon either. Getting comfortable with what is and what is not a statement in Java takes a little bit of practice, and it will come with time, but rest assured, if you do have a statement, it darned well better be followed by a semi-colon, or else the Java compiler will start barking up a storm.

Over time, the nuances of any programming language eventually become commonplace, and that truism applies to Java just as it does to any other programming language on the market. If you’re new to Java, keep these four tips in mind, and if you do run into a compile time error, quickly check to see if your problem was triggered by  the way the Java file itself was named, how the words and letters in the code were cased, or from mismatched brackets and missing semi-colons. Keeping these four problem points at the forefront of your mind will help you troubleshoot your Java code, and hopefully relieve some of the frustrations associated with learning the Java programming language.

 

 

09 Aug 2012

Related Content

Related Resources