ParenthesesOnlyIfArgument
When calling a method or constructor without arguments, it is not necessary to use parentheses.
() are optional for method calls without arguments
() are mandatory even for method calls without arguments
CorrectionHere 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.
OriginWhere could this misconception come from?
Students may have prior knowledge of languages where parentheses are indeed optional for argument-less method calls.
SymptomsHow 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;
ValueHow 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.
Language
Concepts
Expressible In
Related Misconceptions
Other Languages
Literature References
Repository mining study (BlueJ Blackbox)
250000+ students across the world
Qualitative (survey of instructors and students)
Unknown number of undergraduate students, professors, SIGCSE members