java - Passing object into classes, will they just be references to original? -
this question has answer here:
this class contains characters
, objects
within level
.
collisionlistener
interface, , collisionlistener.begincontact()
called whenever character collides object.
i'm wondering, since i'm passing level
collisionlistener
, mean 2 objects stored in memory?
or private level level
inside collisionlistener
reference original level
? how handled internally jvm?
since when modify level
inside collisionlistener
, original level
updated too.
public class level extends world { private player player; private enemy enemy; public level(){ this.setcontactlistener( new collisionlistener(this)); player = new player(); enemy = new enemy(); } /** * move characters */ public update(){ player.update(); enemy.update(); } }
this interface
, thats added level
object. needs reference level
object level.dosomething()
can invoked.
public class collisionlistener implements contactlistener{ private level level; public collisionlistener(level level){ this.level = level; } public void begincontact(contact contact) { this.level.dosomething(); } /* other interface methods .. */ }
or private level level inside collisionlistener reference original level? how handled internally jvm?
of course is. deal references objects , copy those. in terms of c/c++ compare references pointers.
since when modify level inside collisionlistener, original level updated too.
if don't want happen, create copy of level
instance, e.g. using clonable
interface.
Comments
Post a Comment