aspose file tools*
The moose likes Java in General and the fly likes Can anybody Explain the Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can anybody Explain the Program" Watch "Can anybody Explain the Program" New topic
Author

Can anybody Explain the Program

Jayavardhan geepi
Ranch Hand

Joined: Jul 21, 2010
Posts: 66
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

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Aug 16, 2005
Posts: 14270
    
  21

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

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


Understanding is Everything - Peter Lord
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Jul 29, 2010
Posts: 121

Thank you Campbell and Abhimaran. I got it . I was a little confused
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
You're welcome. And sorry for referring you back to your own posts.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Pratik D mehta wrote:Thank you Campbell and Abhimaran.

Who is this? I don't see anyone with this name here!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Jul 29, 2010
Posts: 121

I am sorry for that abimaran
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jul 21, 2010
Posts: 66
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anybody Explain the Program