Document 8
Document 8
As just explained, when you create a class, you are creating a new data type. You can use
this type to declare objects of that type. However, obtaining objects of a class is a two-step
process. First, you must declare a variable of the class type. This variable does not define an
object. Instead, it is simply a variable that can refer to an object. Second, you must acquire
an actual, physical copy of the object and assign it to that variable. You can do this using the
new operator. The new operator dynamically allocates (that is, allocates at run time)
memory for an object and returns a reference to it. This reference is, more or less, the
address in memory of the object allocated by new.
Here, class-var is a variable of the class type being created. The class name is the name of
the class that is being instantiated. The class name followed by parentheses specifies the
constructor for the class. A constructor defines what occurs when an object of a class is
created. Constructors are an important part of all classes and have many significant
attributes. Most real-world classes explicitly define their own constructors within their class
definition. However, if no explicit constructor is specified, then Java will automatically
supply a default constructor. This is the case with Box.