Monday, 4 September 2017

The role of new operator


     
      The Role of “new” operator.

 
The new operator instantiates (from here instantiation of a class refers to creating object of that class) a class by allocating memory for new object and returning a reference to that memory and also it invokes the object constructor for creating and initializing the fields of object.
The new operator requires a single postfix argument: a call to a constructor. The name of the constructor provides a name of the class to instantiate.

 
Consider the example given sample program below.


 Consider the statement,


Rectangle obj = new Rectangle(100,200 );
 
Here Rectangle is the name of the constructor which provides the name of the class to instantiate. So an object of type Rectangle is created in the memory.

The new operator returns a reference to the object it created and this reference is usually assigned to a variable of the appropriate type. In the example shown above the reference is assigned to the variable (here obj ) of type Rectangle.
 

No comments:

Post a Comment

             The `this` keyword in java The this keyword in java can be used for different purposes. Some of these are listed below. ...