File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can we determine whether an object's reference variable is null or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can we determine whether an object Watch "How can we determine whether an object New topic
Author

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

Anurag Malaviya
Greenhorn

Joined: Jul 01, 2009
Posts: 19
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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Nov 01, 2011
Posts: 451
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

Joined: Jul 01, 2009
Posts: 19
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

Joined: Nov 01, 2011
Posts: 451


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.


 
GeeCON Prague 2014
 
subject: How can we determine whether an object's reference variable is null or not?