An
Object in Java is essentially a block of memory that contains space to store
all instance variables. Creating an object is also referred as instantiating an object.
Obtaining
object of a class is a two step process:
o First,
declare a variable of the class type.
o Second,
acquire an actual, physical copy of the object and assign it to the variable.
This can be done by using the new
operator.
o Ex:Myclass
c= new Myclass();
The
above statement combines two steps as shown below:
Myclass c;//Declarereference
to object
c=
new Myclass(); //allocate a Myclass
object
The advantage of using the new operator is allocate memory at run time is that the program can create as many or as few objects as it needed during the execution of the program.
o Since
the memory is finite, it is possible that new
will not be able to allocate memory for an object because insufficient memory
exists. If this happens a run time exception will occur.
Assigning Object Reference
Variables:In Java it is possible to create two or
more references to the same object:
Myclass
c1=new Myclass();
Myclass
c2=c1;
No comments:
Write comments