This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we determine whether an object's reference variable is null or not?

 
Anurag Malaviya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

My doubt is in ques no 12 (chap 3) of kathy siera SCJP book.



In this question, how can we know whether object e1's reference variable e is null or not after we set e1=null ?

added code tags, please read UseCodeTags
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag Malaviya wrote:how can we know whether object e1's reference variable e is null or not after we set e1=null

When an object reference variable is set to null, there's no way to check anything about its member variables, because they go out of scope.
e.g. If an array is null, there is no point in checking if its 'length' is null or not. you anyway can't access it.

If this is related to garbage collection question (I think so), then all you should worry about is - is there at least one reference to that particular object or not.

I hope this helps.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By far the best way to handle this kind of problem is to draw a picture. Represent each instantiated object with a shape such as a circle, oval, amorphous blob or what you will. Draw a line from the object to any & every variable that holds its reference. Get rid of the line by erasing or crossing out, when the variable is re-assigned (including set to null).

Here's a great example of a drawing used to solve a very similar problem:
http://www.coderanch.com/t/539117/java-programmer-SCJP/certification/Garbage-Collection

A similar drawing should show you very clearly whether, after line 11, is there still a reference to the object which e1 used to point to, and whether its member e still points to anything.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag Malaviya wrote:Hi everyone

My doubt is in ques no 12 (chap 3) of kathy siera SCJP book.



In this question, how can we know whether object e1's reference variable e is null or not after we set e1=null ?

added code tags, please read UseCodeTags


I think first of all, e2.e = null will throw a null pointer exception during runtime because e2 = null.
If the program will exit with a null pointer exception at e2.e = e1 , e1=null will never be reached. Therefore, all objects created will be GCed.
 
Anurag Malaviya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helen Ma wrote:
Anurag Malaviya wrote:Hi everyone

My doubt is in ques no 12 (chap 3) of kathy siera SCJP book.



In this question, how can we know whether object e1's reference variable e is null or not after we set e1=null ?

added code tags, please read UseCodeTags


I think first of all, e2.e = null will throw a null pointer exception during runtime because e2 = null.
If the program will exit with a null pointer exception at e2.e = e1 , e1=null will never be reached. Therefore, all objects created will be GCed.


Hi Helen

Thanks a lot for replying. I actually wanted to know that

If i put line 10 that is e2.e=e1 before e2=null then will these references( e3.e = e2;
e1.e = e3;e2.e=e1) exist after call to e2=null;e3=null;e1=null?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I change the code like the above.
When e1, e2 and e3 are reference variables that are set to null, the objects created on line a, b , c have no reference variables refering to them. All the Eco e objects encapusulated by those objects created in line a, b, c will be not reachable as there are no other reference variables refering to them.
For example, if I uncomment out line d, the object refered by e1.e reference variable will not be eligible for GC at line e as there is a reference variable a refering to it. In this situation, althought at line e, e1 is eligible for GCed, the object that was referred by e1.e variable is not as a is now refering to it.


In the exam, when dealing with eligible for GC, pay attention to the object itself created in line 1, 2,3 ... and the variable references a, b, c..... For example,
Eco a is not an object itself. Eco a = new Eco(); means creating an object with a reference variable a.

For another example,


Will a be eligible for GC when play() method exits? My answer is No. a is just a variable, that is not initialized. This code still compiles as long as we never use a to invoke any methods in Integer class.

For another example,
public class Test{
Integer a = new Integer("1");
Integer n ;
public static void main (String...args){
System.out.println("Hello world");

}
}
Will Integer a be eligible for GC when main method finishes? My answer is Yes.
Will n be eligible for GC when main method finishes? My answer is No because n is a reference variable and is initialized as null, n never refers to any object.
Correct me if I am wrong.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic