Sign in to follow this  
nolsen01

[java] Quick and Easy Question

Recommended Posts

I can't seem to get this to stick in my head but: if I create a graphics object ("myGraphics") and then create another Graphics Object ("myOtherGraphics") and set myOtherGraphics = myGraphics.getGraphics(); then, when I make changes to myGraphics, do the same changes happen to myOtherGraphics? or the other way around? in other words, are they referencing the same object. and if not, even though they aren't referencing the same object, does making changes to one, change the other? sorry if this is bit elementary. :)

Share this post


Link to post
Share on other sites

Graphics myGraphics = new Graphics();
Graphics myOtherGraphics = new Graphics();

myGraphics ---------------> Instance #1

myOtherGraphics ----------> Instance #2


myOtherGraphics = myGraphics;

myGraphics --------+------> Instance #1
|
myOtherGraphics ---+ Instance #2

// Instance #2 is garbage collected, since nothing is pointing at it anymore

Quote:
in other words, are they referencing the same object


Yes.

Quote:
myOtherGraphics = myGraphics.getGraphics();


Why myGraphics.getGraphics()? Why not myOtherGraphics = myGraphics; ?

getGraphics() could be returning anything, in which case what they point to depends on how getGraphics is implemented.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this