Declaring Object in Class : Declaring Reference to an Object in Java Programming
- When we create class then we are creating new data type.
- Newly created data type is used to create an Object of that Class Type.
- Creating object is two step process.
Creation of Object involves two steps
- Declaration
- Allocation and Assigning
Rectangle myrect1 = new Rectangle();
this statement is used to create an object we are going to break down this statement in two separate statements –
Rectangle myrect1 ;
myrect1 = new Rectangle();
Step 1 : Declaration of Variable of Type Class
- Above Declaration will just declare a variable of class type.
- Declared Variable is able to store the reference to an object of Rectangle Type.
- As we have not created any object of class Rectangle and we haven’t assigned any reference to myrect1 , it will be initialized with null.
Step 2 : Allocation and Assigning Object to variable of class Type
In short –
- First statement will just create variable myrect1 which will store address of actual object.
- First Statement will not allocate any physical memory for an object thus any attempt accessing variable at this stage will cause compile time error.
- Second Statement will create actual object ranndomly at any memory address where it found sufficient memory.
- Actual memory address of Object is stored inside myrect1.
good job zubair:)
ReplyDeleteGood effort...
ReplyDeleteKeep it up....
Good Collection..
Thanks for appreciate..... i'm working more on this
ReplyDelete....