Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Copy a Class

Mike Parish
Greenhorn
Posts: 10
As a newbie, I wrote this code to test the difference between assigning the values of one object to another by calling the constructor or not.
class Math
{
public static void main(String args[])
{
Operators rtn;
Operators One = new Operators(2,4);
Operators Two = new Operators(4,5);
rtn = One.addFirst(Two);
// Operators fnl = new Operators(rtn); ignore this
Operators fnl = rtn; just a copy? RIGHT
System.out.println("Value of One is : " + One);
System.out.println("Value of Two is : " + Two);
System.out.println("Value of rtn is : " + rtn);
System.out.println("Value of fnl is : " + fnl);
int x = rtn.first;
rtn = new Operators(x, (rtn.ChgSecond(rtn, 5)));
fnl = new Operators(x, (fnl.ChgSecond(fnl, 8)));
System.out.println("Value of rtn is : " + rtn);
System.out.println("Value of fnl is : " + fnl);
}
}
CLASS:
public class Operators
{
// call variables
int first;
int second;
// Constructors
Operators (int val1, int val2)
{
first = val1;
second = val2;
}
Operators (Operators justObj)
{
first = justObj.first;
second = justObj.second;
}
// Methods
public Operators addFirst ( Operators inObject )
{
return new Operators((this.first + inObject.first), inObject.second);
}
int ChgSecond(Operators inObj, int bhm)
{
int chgVal = inObj.second + bhm;
return chgVal;
}
public String toString()
{
return first + ", " + second;
}
}

output:
Value of One is : 2, 4
Value of Two is : 4, 5
Value of rtn is : 6, 5
Value of fnl is : 6, 5
Value of rtn is : 6, 10
Value of fnl is : 6, 5
Here the question: Should the value of 'fnl' be the same as 'rtn'. It's not independent or because it a copy the values can never change unless added about?
Just wondering up here in Canada

Herb Schildt
Author
Ranch Hand
Posts: 253
6
Mike:
I am not sure if I precisely understand what you are asking, but here is an important point to remember: Objects are accessed via references. Thus, when you use a statement such as

The answer to your question is "No". Here is why:
In the statement, both fnl and rtn are references to objects of type Operators. They are not objects, themselves. Thus, the statement assigns to fnl the reference that is contained in rtn. It does not assign a copy of the object referred to by rtn. After the above statement executs, both fnl and rtn will be refering to the same object. Furthermore, this object can be acted upon by either reference.

Mike Parish
Greenhorn
Posts: 10
Hi Herb:
I get it. Now I understand the relationship between reference and object. That explains the outcome of the code. I was getting them confused.
Thanks you for the clarification.
Us old C programmer have a lot to learn.
Thanks again

Gregg Bolinger
Ranch Hand
Posts: 15304
6
Us old C programmer have a lot to learn.
Actually, it shouldn't be a learning process really. Think of it as a pointer without using the pointer syntax. And without having to worry about destroying that object and freeing up memory when you are done using it.