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.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.