I don't get the reason for this warning from NetBeans. My code is as follows
public Class MyClass{
private String value;
//Default constructor
public MyClass(){
...
}
//Copy constructor
public MyClass(MyClass orig){
value = new String (orig.getValue());
}
public String getValue(){
return value;
}
public void setValue(String v){
value = v;
}
}
I don't want side effects when I do:
MyClass a = new MyClass();
a.setValue("hello");
MyClass b = new MyClass(a);
b.setValue("Bye");
As far as I know, if you want to avoid side effects when creating a new object with copy constructors, you have to create a new instance of every non primitive within object.
Strings are not a primitive object but a regular object. Also I realized I can't use orig.clone()
method.
NetBeans warns me with a message like "Remove String constructor invocation". What is the reason of all of this?