Asad Moosvi Asad Moosvi - 5 months ago 19
Java Question

How does the new operator create a new object?

I've just started learning Java and I'm a bit confused about the syntax used for creating new objects.

Take the following:

SomeClass someObject = new SomeClass(1, 2, 3);

What exactly is the syntax that comes after
? Is that the constructor call itself or is it just the class name with some arguments? Does
look at that and know what constructor to call based on those arguments or is the
SomeClass(1, 2, 3)
the constructor call itself?

I would like to know what steps are followed when
is created and what
's role is.

What I don't understand is... is
SomeClass(1, 2, 3)
the actual constructor call on that same line? Or is it called later by


The source of my confusion has probably been the terminology used behind
. As it turns out,
is not an operator at all.
is actually a keyword. That makes much more sense to me now.

This answers my question: Why is new called an operator in Java?

Answer Source

new, the specified Type (SomeClass), the brackets () and possibly arguments etc. are part of one expression, according to the JLS "Class Instance Creation Expressions".

So it does not make sense to separate new and the parts of the expression.

(But the arguments to the constructor are evaluated after memory allocation but before the call to the constructor.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download