• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copy a Class

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic