|
||||||
|
A class definition specifies the characteristics and behaviors of all the objects of that class. The class definition does not create an object. In order to create an instance of a class, the constructor of that class must be called. The syntax for constructing a new instance of a class is:
Things to note:
The above syntax creates an object but does not assign it to a variable. Assigning it to a variable creates a reference to the object that can be used by the rest of the program:
A very powerful technique is to use one variable to reference many different objects at different times:
Sometimes one doesn't need to assign the new instance to a variable to use it. One can immediately pass the new object as an argument of a method that requires an instance of that type of object:
The philosophical statement here is that objects exist whether or not they have a name. In particular, this use of "anonymous" objects is the primary technique used to manage the large numbers of objects generated and used in complex systems. For information on how to write a constructor, see the page on Methods and Classes. |