Section 1 - Constructors Defined


constructor is a method defined in a class that has the same name as the class and no return type. It is responsible for initializing an object by initializing each of its fields.


Here is a constructor for the Box class:



This constructor has input parameters which are assigned to fields. This is called a parameterized constructor. Normally, classes contain a constructor which provides an input parameter for each field.


The constructor is declared with the public modifier so that Box objects can be created and initialized from outside the Box class.


Section 2 - Calling a Constructor


When the new operator creates an object, it calls a class constructor.

 Box b = new Box(1, 2, 3);






Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.