An
Object in Java is a block of memory that contains space to store all the
instance variables. Creating an object
of a class is a two-step process.
1.
Declare a variable of the class type. At this stage, the variable refers to an
object only.
2.
Assigning an actual, physical copy of the object and
assign it to that variable by means of ‘new’ operator. The new operator dynamically allocates memory
for an object and returns a reference to that object.
Example: Rectangle rect1; //
reference
rect1 = new
Rectangle(); // instantiate
(or)
Rectangle rect1
= new Rectangle();
The first statement declares a
variable to hold the object reference.
|
rect1
The second statement assigns the
object reference and allocates memory
|
rect1
Note:
Each object contains its own instance variables of its class. This means that any changes to the variables
of one object have no effect on the variables of another. But the function name is name for both
objects.
It is also possible to create two or
more references to the same object.
Example: Rectangle r1 = new Rectangle( );
Rectangle r2
= r1;
|
r2
No comments:
Post a Comment