Forums Register Login

Can anybody Explain the Program

+Pie Number of slices to send: Send
I have seen this snippet in SCJP form on ranch. But i have confusion.

public class GarbageTest {
GarbageTest g;
int a = 5;
public GarbageTest(Object obj) {
this.g = (GarbageTest) obj;
System.out.println(this.g);
}
public static void main(String[] args) {
GarbageTest gb1 = new GarbageTest(null);
System.out.println(gb1.a);
}
}


Here the instance variable g is assigned a null in the constructor.
But still i can access the variable a. That is last line in the main() prints 5.
How it is? Can anybody explain please.
+Pie Number of slices to send: Send
Please quote where the question comes from.

You still have access to the variable a at the end of the main method. Yes. Why shouldn't you? It doesn't make any difference that you passed null; the gb1 reference and that variable are still there. You are confusing passing null to the constructor and setting the gb1 reference to null.

In fact, you have to pass null somewhere to that constructor to avoid endless recursion and a stack overflow problem
+Pie Number of slices to send: Send
Please UseCodeTags when you post source code.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Please quote where the question comes from.

You still have access to the variable a at the end of the main method. Yes. Why shouldn't you? It doesn't make any difference that you passed null; the gb1 reference and that variable are still there. You are confusing passing null to the constructor and setting the gb1 reference to null.

In fact, you have to pass null somewhere to that constructor to avoid endless recursion and a stack overflow problem



Hello Mr Campbell
I didnt get the overflow , how would overflow occur if null wasnt passed .
Can you please explain in the same example
+Pie Number of slices to send: Send
 

Pratik D mehta wrote:
I didnt get the overflow , how would overflow occur if null wasnt passed .
Can you please explain in the same example


OK, What would happen if you code like below?
+Pie Number of slices to send: Send
 

Abimaran Kugathasan wrote: . . . OK, What would happen if you code like below? . . .

Exactly the sort of thing I meant. And, Pratik D mehta, did you notice I didn't post "a stack overflow problem", but "a stack overflow problem"? Did you read the link?
+Pie Number of slices to send: Send
Oh, that was your question I was referring to in the first place.

If you don't pass null, you have a second GarbageTest object, which you pass a 3rd GarbageTest object, and that requires a 4th GarbageTest object, and that requires a 5th . . . .
+Pie Number of slices to send: Send
Thank you Campbell and Abhimaran. I got it . I was a little confused
+Pie Number of slices to send: Send
You're welcome. And sorry for referring you back to your own posts.
+Pie Number of slices to send: Send
 

Pratik D mehta wrote:Thank you Campbell and Abhimaran.


Who is this? I don't see anyone with this name here!
+Pie Number of slices to send: Send
 

Abimaran Kugathasan wrote: . . . I don't see anyone with this name here!

You should see the way they misspell other people's names. Or call the women "Sir".

Pratik D mehta, please be careful; it is often regarded as rude to spell somebody's name wrongly, when it is visible written. Use copy-and-paste for names as well as for code.
+Pie Number of slices to send: Send
I am sorry for that abimaran
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:You should see the way they misspell other people's names. Or call the women "Sir".



Thanks Campbell Ritchie, But, Women??

And, Pratik D mehta, I just indicate it. No need of Sorry!
+Pie Number of slices to send: Send
Hey.. Yes I was confused because of null.
Although I'm accessing the variable 'a' through gb1 which is not null and g is null. So NullPointerException kicks me if I use 'g' to access 'a'.

Thanks guys... One question has led another with answer.. Great...
+Pie Number of slices to send: Send
 

Abimaran Kugathasan wrote:Thanks Campbell Ritchie, But, Women??


Just recently someone called Jeanne Boyarsky "sir". I had to correct the person to use "ma'am" instead.
+Pie Number of slices to send: Send
I also apologize to Campbell Ritchie . I guess I was confused at that time and thinking many things . I got things wrong .
I Respect the help I get on this forum . All the people here are amazing and it feels very friendly.
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 885 times.
Similar Threads
doubt about GC
Confusion in Garbage Collection
Garbage Collection
Garbage Collection Program
Help in understading the GC question from ExamLab.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:16:19.