Making Copies of Objects


As we mentioned in the "Objects" section, an object is an instance of a class. A variable that refers to an object will store a reference to the location in memory where the object is stored. To make a copy of an object, you might think to declare a new reference type variable and assign it to the original variable. For example, if you wrote a class named Gas and wanted to make a copy of a Gas object, you might think you should type ”

 Gas oldGas = new Gas("air"); Gas newGas = oldGas; 

However, this does not create two independent objects. What this has done is create two references to the same Gas object. To create an independent copy of the original Gas object, you would need to create a distinct Gas object and then copy the values of the fields of the original Gas object into the copy.

Example: Copying Objects

To demonstrate how to correctly access objects, we are first going to modify the SimpleGas3.java program to add methods that can be used to change the values of the pressure and temperature variables .

 public class SimpleGas4 {   private double pressure;   private double temperature;   public SimpleGas4() {     pressure = 101325.0;     temperature = 273.0;   }   public SimpleGas4(double p, double t) {     pressure = p;     temperature = t;   }   // These methods return the values of the   // pressure and temperature variables   public double getPressure() {     return pressure;   }   public double getTemperature() {     return temperature;   }   // These methods change the values of the   // pressure and temperature variables   public void setPressure(double p) {     pressure = p;     return;   }   public void setTemperature(double t) {     temperature = t;     return;   } } 

The GasDriver4 class creates three variables that reference SimpleGas4 objects. The second variable is made to reference the same object as the first. The first and second variables will share the same copies of the pressure and temperature fields. The GasDriver4 class then makes a SimpleGas4 object that is an independent copy of the first object. One way to make an independent copy is to pass the current values of the first object's fields to the SimpleGas4 class constructor. The two variables, although they refer to objects with the same initial state, are completely independent.

 public class GasDriver4 {   public static void main(String args[]) {     //  Declare two SimpleGas4 variables. Create a     //  SimpleGas4 object and assign it to the first     //  variable. Make the second variable reference the     //  same object.     SimpleGas4 gas1 = new SimpleGas4(21.95, 207.8);     SimpleGas4 gas2 = gas1;     System.out.println("pressure of Gas 1 is " +                         gas1.getPressure());     System.out.println("pressure of Gas 2 is " +                         gas2.getPressure());     //  The two SimpleGas4 variables point to the same     //  object. Either variable can be used to change     //  the object's pressure field.     gas2.setPressure(19.2);     System.out.println("\npressure of Gas 2 is " +                         gas1.getPressure());     System.out.println("pressure of Gas 1 is " +                         gas1.getPressure());     //  Another SimpleGas4 variable is declared.  The     //  values from gas1 object are copied into a new     //  SimpleGas4 object.     SimpleGas4 gas3 = new SimpleGas4(            gas1.getPressure(), gas1.getTemperature());     System.out.println("\npressure of Gas 3 is " +                          gas3.getPressure());     //  The objects referenced by gas1 and gas3 are     //  independent. Changing the pressure of the gas3     //  object does not affect the gas1 object.     gas3.setPressure(23.4);     System.out.println("pressure of Gas 3 is " +                          gas3.getPressure());     System.out.println("pressure of Gas 1 is " +                          gas1.getPressure());   } } 

Output ”

 pressure of Gas 1 is 21.95 pressure of Gas 2 is 21.95 pressure of Gas 2 is 19.2 pressure of Gas 1 is 19.2 pressure of Gas 3 is 19.2 pressure of Gas 3 is 23.4 pressure of Gas 1 is 19.2 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net