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

Can anybody Explain the Program

 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when you post source code.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . . .
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell and Abhimaran. I got it . I was a little confused
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. And sorry for referring you back to your own posts.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta wrote:Thank you Campbell and Abhimaran.

Who is this? I don't see anyone with this name here!
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for that abimaran
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic