The new
operator is used to create an instance of a class. What’s happening in new Bus(new PrototypeEngine1()).drive()
is that an instance of PrototypeEngine1
is being created as an argument to new Bus()
.
To illustrate this concept, let’s refactor the single line of code in step by step manner:
PrototypeEngine1 engine = new PrototypeEngine1();
Bus bus = new Bus(engine);
bus.drive();
These lines of code are practically equivalent to that original one line of code.
solved How is it possible to use a “new” as parameter in side another new?