Summary of Class Presentation for June 16, 1999 Topic: Initialization Java provides constructors just like those in C++. Whenever an object is created (whenever the new operator is used), a constructor is called for that object. If the class of that object does not contain any constructor at all, the Java compiler produces a default constructor which initializes each field of the object to the default value for that type of data ( these are 0 for all the numerical primitive types, the null string for char, and false for booleans). If the class contains at least one constructor, the compiler does not produce a default constructor. If the class contains at least one constructor, you must pass parameters in the new clause appropriate to one of the constructors that are present in the class. This is complicated a little, however, by the automatic promotions that Java will do when necessary. These were discussed with the primitive data types in an early class meeting, but basically they fall into two chains: byte to short to int to long, and float to double. The vocabulary here is a little confusing. We will use the following terms in this course: 1. object variable: the variable actually declared with the declaration ( for example String s; Here s is the object variable ); 2. object: the storage setup by a new. This storage has several pieces including: the data fields of the object, a reference to the data fields of the class, and a reference to the methods this object implements; 3. default constructor: a constructor taking no arguments. This includes both the automatically created constructor used when the class contains no constructors, and any programmer-written constructor that takes no arguments. The order of activities when a new is invoked is: 1. storage is allocated for the object; 2. the appropriate constructor is determined and called based on the types of the actual parameters passed by the new clause. Here is a simple example: Sample sam = new Sample(10, w+2); Here sam is the object variable. The object is set up by the new clause which is new Sample(10, w+2) The arguments to the constructor are 10 and w+2.