Consider following situation:
I write a java program for an embedded(modem) device. The SDK doesn’t offer Cloneable.
Therefore I have in class GsmSignalStrength the method clone(), but it’s not
from Object.clone(), it is “my” implementation.
I would like to know is this useful and correct or should I use a copy constructor in GsmSignalStrength, like in the comment?
I don’t see the advantage of a copy constructor, because I will have to create another GsmSignalStrength object in my StateMachine.
Here is my code…
File 1 “Status.java”:
class Status
{
Status(GsmSignalStrength gsmSignalStrength)
{
initClass(gsmSignalStrength);
}
GsmSignalStrength gsmSignalStrength;
private void initClass(GsmSignalStrength gsmSignalStrength)
{
this.gsmSignalStrength = gsmSignalStrength;
}
}
File 2 “GsmSignalStrength”:
class GsmSignalStrength
{
GsmSignalStrength(byte signalStrength)
{
initClass(signalStrength);
}
GsmSignalStrength()
{
initClass(100);
}
byte value;
public void copyTo(GsmSignalStrength destination)
{
destination.value = this.value;
}
public GsmSignalStrength clone()
{
GsmSignalStrength clonedValue = new GsmSignalStrength();
this.copyTo(clonedValue);
return clonedValue;
}
/* With copy constructor
GsmSignalStrength(GsmSignalStrength gsmSignalStrength)
{
gsmSignalStrength.value = value;
}
*/
private void initClass(byte signalStrength)
{
this.value = signalStrength;
}
}
File 5 “GsmModemHandler”:
class GsmModemHandler
{
GsmModemHandler()
{
initClass();
}
private GsmSignalStrength m_gsmSignalStrength;
GsmSignalStrength getGsmSignalStrength()
{
...bla...
return m_gsmSignalStrength;
}
private void initClass()
{
m_gsmSignalStrength = new GsmSignalStrength();
}
}
File 4 “StateMachine”:
class StateMachine
{
StateMachine(GsmModemHandler gsmModemHandler)
{
initClass(gsmModemHandler);
}
private GsmModemHandler m_gsmModemHandler;
void doSomething()
{
GsmSignalStrength gsmSignalStrength = m_gsmModemHandler.getGsmSignalStrength();
Status xy = new Status(gsmSignalStrength.clone());
/* With copy constructor
GsmSignalStrength gsmSignalStrengthCopy = new GsmSignalStrength(gsmSignalStrength);
Status xy = new Status(gsmSignalStrengthCopy);
*/
}
private void initClass(GsmModemHandler gsmModemHandler)
{
m_gsmModemHandler = gsmModemHandler;
}
}
What you
havehad in that comment,is not a copy constructor. The line
has no effect at all because all it does is move a method-local reference to point to
this.A functioning “copy constructor” would be something like
the point being to create an independent object with the same member variables as the original.
Also, unlike in C++, writing
does not invoke a copy constructor. All it does is copies the reference — you end up with two references pointing to the same object.
If you want to create a new object you have to invoke the constructor explicitly:
As of whether it is “useless”, that depends on your application. A copy constructor, a
clone()method, and yourcopyTomethod all accomplish the same basic task. You can create a new object with the constructor, or withclone(), or create a new object and usecopyToon it — pick one way to do what you want and stick with it.