In OOPs Series, i have explained Class and Object. But to use an object and to get the work done first we need to create an object by considering Class as an template for Creating an object in java.
As an object is an instance of a class, so by using our understanding we can say, creating an instance of class is called as instantiation.
In java there are different ways through which we can create an object like ...
- using new keyword.
- using class.forName()
- using clone() method
- Using object desirialization.
- using ClassLoader mechanism
Apart from that if we dive much deeper then there is also a design pattern called as Factory Design Pattern, which is also used.
But every technique has its own importance and there is some boundation bounds with them.
Here I will explain about using new keyword for creating an object. In upcoming post i will Explain about all other techniques.
Example :
package in.technoscience.first;
1. public class ObjectCreation {
2. public static void main(String[] args) {
3. ObjectCreation oc = new ObjectCreation();
}
}
(reference At line no 3: variable) (call to class constructor)
ObjectCreation oc = new ObjectCreation()
(classname) (keyword)
To understand the instantiation process try to correlate it with remote and television.
consider television as an object and remote as an reference. as long you are holding the reference you have connection with the television. as soon as one says to change the channel or volume you work with reference which modifies an object. And if you want to move to different place you move with remote (reference ) not with the television (object).
same way in above code ObjectCreation oc is the reference to the object created by the statement new ObjectCreation() pointed by right hand side of assignment operator.
consider television as an object and remote as an reference. as long you are holding the reference you have connection with the television. as soon as one says to change the channel or volume you work with reference which modifies an object. And if you want to move to different place you move with remote (reference ) not with the television (object).
same way in above code ObjectCreation oc is the reference to the object created by the statement new ObjectCreation() pointed by right hand side of assignment operator.
No comments:
Post a Comment