Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Object constructor called when creating an array in Java?

In Java, an array IS AN Object. My question is... is an Object constructor called when new arrays is being created? We would like to use this fact to instrument Object constructor with some extra bytecode which checks length of array being constructed. Would that work?

like image 746
Peter Štibraný Avatar asked Jan 23 '09 22:01

Peter Štibraný


2 Answers

Per the JVM spec: "Arrays are created and manipulated using a distinct set of instructions." So, while arrays are instances of Objects, they aren't initialized the same way that other objects are (which you can see if you scroll up from that link anchor).

like image 111
kdgregory Avatar answered Sep 26 '22 14:09

kdgregory


As far as the Java Language Specification is concerned, although both use the new keyword, Class Instance Creation Expressions and Array Creation Expressions are different forms of expression, each with its own rules. The description of Array Creation Expressions does not mention calling a constructor.

like image 44
Patricia Shanahan Avatar answered Sep 22 '22 14:09

Patricia Shanahan