January 2011
Intermediate to advanced
1648 pages
70h 30m
English
During the creation of a new instance of a class, one often wants to run some code to initialize the state associated with the instance being created. Examples include setting fields or running other initialization tasks.
As such, constructors are special kinds of methods that cannot be called directly but are invoked through the use of the new operator. Parameters can be used on a constructor definition to pass data to the newly created object. To declare such an instance constructor, you define what looks like a method but has the name of the class and omits a return type:

The constructor shown here will be called when ...
Read now
Unlock full access