MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
    • Getting Started
    • The main method
    • How Programs Work
    • More on methods
    • Recap and Key Terms
    • Newbie pitfalls
    • Eclipse
    • Unit 0 Slides
  • Unit 1: Using Objects and Methods
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections

Pitfalls of a Newbie!

Here are some common pitfalls for newbies in Java programming. Try to avoid them by keeping a keen eye for detail.

Good coding practices not followed

Coding practices are language-specific. In Java, there are several fundamental coding styles you should follow. While the Java compiler will accept code that does not follow these practices, other programmers will find it difficult to read and maintain.

  • Class names starting with a lowercase letter or not using a noun form: In Java, class names should always be nouns and start with an uppercase letter.

    AVOID: class calculateInterest RECOMMENDED: class InterestCalculator

  • Method names starting with an uppercase letter or not using a verb form: Experienced programmers name methods using verbs and start them with a lowercase letter. Method names should use camelCase when they consist of multiple words.

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

  • Code not indented correctly: Although the Java compiler does not care about indentation, humans will find your code difficult to read. Most IDEs can handle indentation automatically, but you should ensure it remains consistent during fine-tuning.

Common syntax errors

With syntax errors, the compiler cannot transform your .java file into a .class file.

Syntax Errors
  • Syntax errors are mistakes in the use of the Java language rules (the "grammar" of the code). They are caught by the compiler at compile-time.

Sometimes the compiler provides accurate error messages with the exact line number. In other cases, the reported line number might be misleading, pointing to a line before or after the actual error.

  • Typos in method, variable, or class identifiers: This is the most common error for new programmers. A method might be declared with one name but invoked with a typo or incorrect casing. Remember, Java is case-sensitive! If the compiler claims a member is missing but you know it exists, check for typos or case mismatches. To avoid this, use your IDE's autocompletion feature. Most modern IDEs suggest identifiers after you type the first few letters.

  • Missing or extra curly braces: A programmer might start a code block with a left curly brace { but forget the closing brace } (or vice-versa). This can happen in class or method definitions. While most IDEs help manage braces, you must still ensure they are balanced. The compiler will report an error, but the message may be misleading, making the root cause difficult to find.

  • Incorrect main method: The signature for a standalone program must be public static void main(String[] args). While public and static can be interchanged, the other keywords are mandatory. If there is a typo or a missing modifier, the compiler or JVM will report that it cannot find the main method. If you are certain it exists, double-check its signature for errors.

  • Method invocation with the wrong argument list: A method must be called with the same number and types of arguments as defined in its parameters. If there is a mismatch, the compiler will report an error. Always check the console for the error message and line number to help diagnose the issue.

  • Confusing curly braces with parentheses: Class definitions and method bodies use curly braces { }, while method parameters and control structures use parentheses ( ). Mismatches will result in syntax errors.

  • Method declared outside the class: This is a frequent mistake where a method is defined outside the class's closing curly brace. Compiler errors for this can be misleading.

    Incorrect method declaration:

Task: Identifying a method declared outside its class. ```java class Calculator {

}


// The method block is outside of the class block!


public static int calculate() {
    // other statements go here
}
```
  • Method declared inside another method: Java does not allow nested method definitions. All methods must be defined directly within the class body, not inside other methods.

    Incorrect method declaration:

Task: Identifying an illegal nested method declaration. ```java class MyClass { public void method1() { public void method2() { // This is not allowed.

        }
    }
}
```
Privacy Policy | Terms & Conditions