ParenthesesOnlyIfArgument

Misconception:

When calling a method or constructor without arguments, it is not necessary to use parentheses.

Incorrect

() are optional for method calls without arguments

Correct

() are mandatory even for method calls without arguments

Correction
Here is what's right.

In Java parentheses are required to distinguish method calls (o.m()) from field accesses (o.f).

When calling any method, even a method without arguments, parentheses are required.

Origin
Where could this misconception come from?

Students may have prior knowledge of languages where parentheses are indeed optional for argument-less method calls.

Symptoms
How do you know your students might have this misconception?

A student may write code like this:

class C {
  int f(int x) { ... }
}

C o = ...;

int result = o.f;

Value
How can you build on this misconception?

At first this misconception may look purely superficial. However, it can be surprisingly deep.

In languages where functions are values, it is essential to understand the difference between accessing the function as a value (f), and invoking a function (f()). With the addition of lambdas and method references to Java, Java now also supports treating functions as values. However, in Java—unlike in other languages with first-class functions—to invoke such functions, one needs to use the traditional method invocation mechanism to call the single method provided by the functional interface that represents the function’s type:

Function<Integer,Integer> f = ...;`
int y = f.apply(x); // instead of f(x) used in other languages

An additional potentially valuable aspect of this misconception is that it might indicate that a student sees methods and fields as something similar. And indeed, in some languages (like JavaScript) objects are seen primarily as a general map from keys to values (sometimes called a “hash”). Some entries in that map can be seen as “methods”, because their values are functions.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.