Reassigning a Reference Variable

Reassigning a Reference Variable

Its also a decouple a reference variable from an object by setting the reference variable to refer to another object.we have a following code : 
 class GarbageProg {
 public static void main(String [] args) {
StringBuffer a1 = new StringBuffer(\"hello this is my first garbage programe \");
StringBuffer a2 = new StringBuffer(\"welcome to my progarme \");
System.out.println(a1);
// At this point the StringBuffer \"hello this is my first grambage progame \" is not eligible
a1 = a2; // Redirects a1 to refer to the \"welcome to my programe \" object
// Now the StringBuffer \"hello this is my first garbage programe \" is eligible for collection
}
}

When we are created an object then we need to the method.When the method is invoked the local variable is automatically created exist only for the duration of the method.Once the method has returned, the objects created in the method are eligible for garbage collection.

By:umang
Date:2014-03-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related scjp1.5 chap8 Links

scjp1.5 chap8 interview questions and answers for experienced and fresher