^

Creation Of Object

Creation of an object of a class is also known as instantiation process. Instantiation process is completed in 3 steps:
  1. Declaration of reference variable.
  2. Creation of object.
  3. Assign the reference to variable.
  1. Declaration of Reference Variable:
  2. String S;
    Tells the JVM to allocate the space to a reference variable S. This reference variable may be a Local Reference Variable(LRV) or Instance Reference Variable(IRV). If it is a local reference variable, it will occupy space on stack whereas an instance reference variable occupies space within an instance on heap.
  3. Creation of Object:
  4. new String (“India”);
    Its an instruction to the JVM for allocating space to the object on the heap.
  5. Linking the Object and Reference Variable (=):
  6. String S = new String (“India”);
    The assignment operator (=) gives the control of object to reference variable by assigning its address. Now, we can call methods and use member variables using the reference variable. The address contained by S is known as reference value.

    Scope and Life Time of Variables

    Scope
    Scope of a local reference variable(LRV) is limited as compare to instance reference variable(IRV), because, it can be used within the particular method, whereas, the IRV is accessible within all the methods of a class.
    Life
    Instance reference variable(IRV) will be retained in the memory till the containing object is alive. An object can stay alive till it is having reference variable or if no reference variable exist then until it is garbage collected.

    Array of Objects

    Arrays occupy storage space on heap e.g.
    String list [] = new String [3];
    The above line of code will not create array of objects, in fact it will create an array of reference variables on heap and a reference variable i.e. list on stack as shown below:
    To create three objects on heap(as shown below), write following lines of code:
    list[0] = new String (“Sandy”);
    list[1] = new String (“Mandy”);
    list[2] = new String (“Jony”);
    

    Passing Object & Returning Object

    It will be strange to know that objects cannot be passed as argument and returned also. Beacause, in both cases, only references are copied from one method to another. Objects are placed on heap, just their control is passed from method to method. A receiver method can change the state of object received as argument. When the objects are passed or returned then just their references are copied, so it can be said that objects are passed by value. For better understanding consider the following program to get the difference of heights of two persons:
    For Example:
    class Height
        {		
    	int feet,inches;
    	Height(){} 
    	Height(int f,int in)
        	{  
        	   feet=f;
    	   inches=in;	 
        	}
    	Height getDifference(Height ref)
    	 {    
         	  int df=Math.abs((feet*12+inches)-(ref.feet*12+ref.inches));
    	  Height dfObj= new Height(df/12,df%12);
    	  return (dfObj);  	 
             }
    	public String toString()
    	  {	
          	    return(“Feet:”+feet+” Inches:”+inches);
    	  }
        }
    class PasRetOb 
          {     
            public static void main(String args[])
             {	
              Height aman,raman;
              aman=new Height(5,8);
              raman=new Height(4,7);
              Height diff=raman.getDifference(aman);
              System.out.println("Difference:"+diff);
            }
         }
    

About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
17 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android. If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : [email protected] #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap