aspose file tools*
The moose likes Beginning Java and the fly likes Copy a Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copy a Class" Watch "Copy a Class" New topic
Author

Copy a Class

Mike Parish
Greenhorn

Joined: Oct 24, 2003
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

Joined: Oct 01, 2003
Posts: 239
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.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Mike Parish
Greenhorn

Joined: Oct 24, 2003
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    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.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy a Class