Steps to create an object in JAVA.
Steps involved in creating an object in JAVA.
1.Create an address variable.
2.Using a new operator allocate the memory for the required object.
3.Address returned by the new operator must be collected b the address variable.
For Example consider the code
class Dog{
String name;
String color;
int cost;
}
class LaunchTheDog{
public static void main(String[] args){
Dog d1; // Step 1
//Step 3;
d1=new Dog();
//Step 2;
d1.name="Ramu";
d1.color="White";
d1.cost=10000;
System.out.println(d1.name);
System.out.println(d1.color);
As soon as you call the "new" keyword the JVM provides a block of memory for us with a address.
After providing the address it directly goes to the class also called (blueprint or Plan) and stores all the variable ,in other words the properties into the allocated memory one by one.
In this case the variables such as the name,color,cost are stored inside the memory.
Correspondingly the address consider it as 1000H (some random number) obviously it will be of the type Dog same as the class name.
NOTE: Consider the code snippet "Dog d1";
If asked "Is this the object of Dog class" the answer would be straight away "NO" because the variable d1 just contains the address of the block and would be pointing to the corresponding block used by the JVM to store the variables and would be acting as a pointer.
So overall whenever you call the keyword "new" it is at this time the JVM creates the corresponding object.
1.Create an address variable.
2.Using a new operator allocate the memory for the required object.
3.Address returned by the new operator must be collected b the address variable.
For Example consider the code
class Dog{
String name;
String color;
int cost;
}
class LaunchTheDog{
public static void main(String[] args){
Dog d1; // Step 1
//Step 3;
d1=new Dog();
//Step 2;
d1.name="Ramu";
d1.color="White";
d1.cost=10000;
System.out.println(d1.name);
System.out.println(d1.color);
System.out.println(d1.cost);
}
}
}
}
First for the variable d1 a memory block would be allocated and it would just contain the address of the bigger block created by JVM ,in this case lets choose 1000H as the address. So here d1 is called as the address variable shown in Step 1.
After providing the address it directly goes to the class also called (blueprint or Plan) and stores all the variable ,in other words the properties into the allocated memory one by one.
In this case the variables such as the name,color,cost are stored inside the memory.
Correspondingly the address consider it as 1000H (some random number) obviously it will be of the type Dog same as the class name.
NOTE: Consider the code snippet "Dog d1";
If asked "Is this the object of Dog class" the answer would be straight away "NO" because the variable d1 just contains the address of the block and would be pointing to the corresponding block used by the JVM to store the variables and would be acting as a pointer.
So overall whenever you call the keyword "new" it is at this time the JVM creates the corresponding object.
Comments
Post a Comment