Pitfalls of a Newbie!

Here are some of the common pitfalls of newbies in Java Programming. Try to avoid them by keeping a keen eye for details.

Good coding practices not followed

Coding practices are language specific and in Java there are a few fundamental coding style that you should follow. Java compiler is happy with your code even if you did not follow good coding practices. However a programmer who follows good coding practices for Java, will not be happy seeing the following:

  • Class name starts with a lowercase letter and/or is not a noun form: In other programming languages this may be in vogue but not in Java.

    AVOID: class calculateInterest
    RECOMMENDED: class InterestCalculator

  • Method name starts with an upper case and/or is not a verb form: An experienced programmer is careful in naming the method with a verb form and starts with a lower case letter. And the method name is camelCased when multiple words are used.

    AVOID: public int Calculator()
    RECOMMENDED: public int calculate()

  • Code not indented correctly: Many IDE's take care of the indentation for us but when you are fine tuning your code there are chances that the indentations get messed up. Java compiler does not care, but other humans will have a hard time reading and understanding your code.

Common syntax errors

In these types of errors, the compiler is unable to compile your .java file into a .class file. In some cases the compiler dumps accurate error messages on the console with the exact line number of the error. In some other cases, although the compiler is unable to compile due to errors, the line number shown on the console may be misleading and is not the exact line of error, but instead is either before or after that line. It gets tricky in such situations. Here are some common compile time errors by newbies.

  • Typo in the method, variable or class identifiers while referencing: This is by far the most common error I see with new programmers! A method is declared with a name but when it is invoked from elsewhere in the program there is either a typo or the case does not match. Remember, Java is case sensitive! All identifiers should match exactly. When the compiler complains a method, variable or class is not present and you know it is present, check the typo or case. The best way to avoid this error is to take the IDE's help in completing the identifier literal when referencing. Once declared, most of the modern IDE's can help in auto completing the identifier if you key in the first few letters of the identifier, using the shortcut for autocomplete. Check with your IDE manual to figure out the specific shortcut. In Android Studio, as soon as you start typing one letter of your identifier, autocomplete prompt is shown with all the identifiers which start with the given letter(s). Choose that to finish your identifier.

  • Missing or extra right/left curly braces: Starts a left curly brace '{', for block of code but forgets to end the block with a closing right '}' curly brace or vice-versa. This could be for the class, method definitions or any other block of code. Although this mistake could be avoided when using most of the IDE's, it is still important to keep an eye on the left and right curly braces and where they start and end. Compiler will not let you go further with this error though. However sometimes the compiler errors thrown, due to this mistake, is misleading and it is hard to identify the root cause.

  • Incorrect main method: The signature of the main method is public static void main(String[] args) for a standalone program. If there is a typo or missing/incorrect modifiers then the compiler complains that it cannot find the main method to run. When you know that the main method is present in your class, check for typos or missing/incorrect modifiers for your main method.

  • Method invocation with wrong argument list: A method should be invoked with the same number of arguments as are declared as method parameters. Compiler complains with the cause. Always look into your console and read the errors/exceptions and line number of the code where the error/exception occurred, to get a clue for fixing your mistakes.

  • Method declaration is not inside the class: This also happens quite frequently with new programmers. A method is declared outside of the class's right curly brace. Compiler errors may be misleading sometimes on this mistake too.

  • Using curly brace in place or parenthesis and vice-versa: A class definition is inside a pair of curly braces. A method's parameters are defined inside a pair of parenthesis and method's statements are inside curly braces. Any mismatch will throw errors.

class Calculator{

// method block is outside of the class block!! wrong!
public static int calculate(){
    // other statements go here

results matching ""

    No results matching ""