How Objects Are Created

In the preceding programs, the following line was used to declare an object of type Vehicle:

Image

This declaration performs two functions. First, it declares a variable called minivan of the class type Vehicle. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, the declaration creates a physical copy of the object and assigns to minivan a reference to that object. This is done by 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 ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.