COM1204 Summer 2003 "Top 10 Java problems" - Prof. Futrelle

August 18


As of the completion of the second quiz, there are still a number of problems that people are having with writing code. Many of them are really not specific to Java, but have to do with creating instances (constructors), defining and calling methods, assigning types, etc. Below are some of the more prominent misconceptions.

  1. All methods with a non-void return type must include a return statement returning a primitive or an object that matches the return type of the method. (It can also return a subtype.)

  2. Every field, whether a variable or a method, must have a type designator. "abstract" is not a type. "overridden", which various people have used, is not even a keyword in Java.

  3. "this" is rarely used and rarely needed. Its most common use is when a method argument name is identical to a method field name, e.g., boo(int a){this.a = a;}

  4. Constructors must be defined with an arglist "(....)" and used with an arglist. Even if the arglist is empty, "()", it must still be there.

  5. The header of a class or interface definition should not have any parenthesized element. No "arglist" is involved at this point.

  6. The only items allowed in a class definition are variable fields, possibly with initial values, and method definitions. Placing arbitrary executable code in a class that is not an initializer on inside a method, is not allowed.

  7. The definition of main() must always be of the form
    public static void main(String[] args) { ... }
    or
    public static void main(String args[])
    where "args" can be any variable name you choose.

  8. The declaration of a variable field in a method does not require initialization.

  9. Methods are called with dot notation as blah.meth(), where blah is a variable that is an instance of a class and meth() is some method defined for that class, directly, or by inheritance. References to static fields are similar and are of the from SomeClass.smeth() where SomeClass is the name of a class and smeth() is the name of a static method. The same notation applies to direct reference to variable fields or static variable fields.

  10. Constructors have no return type.