java - What object calls the constructor? -


this question has answer here:

point point = new point (30, 20); 

point(int, int) here constructor of point class, default not private , not static, point object needs calling it, above not being called method of point object, instead new operator. new operator other allocating memory? automatically create empty point object , call constructor method of point object? if make constructor private mean thenew operator won't work anywhere else except inside class?

what happens @ runtime when invoking constructor defined in the jls. here's partial quote:

next, space allocated new class instance. if there insufficient space allocate object, evaluation of class instance creation expression completes abruptly throwing outofmemoryerror.

the new object contains new instances of fields declared in specified class type , superclasses. each new field instance created, initialized default value (§4.12.5).

next, actual arguments constructor evaluated, left-to-right. if of argument evaluations completes abruptly, argument expressions right not evaluated, , class instance creation expression completes abruptly same reason.

next, selected constructor of specified class type invoked. results in invoking @ least 1 constructor each superclass of class type. process can directed explicit constructor invocation statements (§8.8) , specified in detail in §12.5.

the value of class instance creation expression reference newly created object of specified class. every time expression evaluated, fresh object created.

and indeed, if constructor private, won't usable outside of class itself.


Comments