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