Join Charles Kelly for an in-depth discussion in this video Exploring syntax checking, part of Learning Eclipse (2012).
In order to illustrate syntax checking in the Java Editor, I've taken the code from our previous Person Class and I've renamed it PersonWithErrors. As you can see if you look at the right gutter, there are many error markers. What we can do is we'll correct these one at a time and we'll show how the syntax errors are illustrated and what we can do to correct them. The first error that the editor finds for us is right here on line 8. The pop up says Date cannot be resolved to a type. If I click here I'm given the option to use an Import statement.
I'll create the Import statement using java.util and we see import java.util.Date is now included in our code. If I highlight our next error, I'll come down to this statement on line 23. String literal is not properly closed by a double quote ("). I'll add a closing double quote and I'll save the file and we still have an error indicator. What's the problem? Well, if I highlight it one more time, Syntax error, insert ";" to complete BlockStatement.
If I add a semicolon (;), the error decorator goes away. If I go down to line 26, GregorianCalendar cannot be resolved to a type. This requires another import statement. I'll import GregorianCalendar from java.util. On line 28, calendar cannot be resolved to a variable. If I double click, Import 'Calendar' from java.util, I see that we have one more error. If dob, date of birth, equals 0, Type mismatch: cannot convert from Date to boolean, Type mismatch: cannot convert integer to Date.
So the syntax checking editor features within the error are telling us that there's a problem, but it can't resolve the problem for us. In particular, if you look carefully what's happening is we're using an assignment statement and attempting to assign the value of 0 to date of birth, when really what we want to say if date of birth is equal to 0. If I rephrase this is as if null is equal to date of birth, the error indicator goes away.
As a point of reference, I think it's always a good idea when you're doing a test between a constant such as null and a variable to place the constant on the left-hand side of the conditional operator. In some languages such as scripting languages and functional programming languages, it's syntactically correct to include an assignment statement within an if statement, that's the reason that you always want to include the constant on the left-hand side of the operator to avoid problems in these types of languages.
The final thing that we see if we look in the right-hand gutter is a blue indicator. It's neither an error nor a warning. It's telling us that we've included a TODO here. If I replace the TODO indicator with an error message from the exception, the blue box in the right-hand gutter goes away.
- Downloading and installing Eclipse for Mac or Windows
- Exploring the toolbar
- Creating a new project
- Changing views and perspectives
- Using the editor
- Working with JUnit and templates
- Using Javadoc
- Using the code completion features
- Exploring syntax checking
- Getting help with Eclipse
- Extending Eclipse