x = 3; y = 5 x = 45; y = 83
As the example program shows,
a MyPoint
object can be changed by any method that has a reference to it.
Note: If a reference to a MyPoint
object is passed
as a parameter, then the invoked method can use that reference to change the
public instance variables of the object.
Here is the example program with another class:
class MyPoint { public int x=3, y=5 ; public void print() { System.out.println("x = " + x + "; y = " + y ); } } class PointDoubler { public void twice( MyPoint parm ) { System.out.println("Enter PointDoubler"); parm.print() ; parm.x = parm.x * 2 ; parm.y = parm.y * 2 ; parm.print() ; System.out.println("Leave PointDoubler"); } } class PointTester { public static void main ( String[] args ) { MyPoint pt = new MyPoint(); PointDoubler dbl = new PointDoubler(); pt.print(); dbl.twice( pt ); pt.print(); } }