A Closer Look at new

The new operator has this general form:


classVariable = new classname( );

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.

Assigning Object Reference Variables

Consider the following code:


Box b1 = new Box(); 
Box b2 = b1;

b1 and b2 will refer to the same object.

Any changes made to the object through b2 will affect the object b1. If b1 is been set to null, b2 will still point to the original object.


Box b1 = new Box(); 
Box b2 = b1; 
// ... 
b1 = null;

Assigning one object reference variable to another only creates a copy of the reference.

Home 
  Java Book 
    Class  

Class Creation:
  1. Introducing Classes
  2. A Simple Class
  3. A Closer Look at new
  4. Variables and Initialization
  5. this Keyword
  6. A Demo Class
  7. The Person class: another demo
  8. Understanding static