0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
perere
  • 5
  • 4
  • 2
    `String` is immutable. You essentially never need to copy it. – Andy Turner Apr 15 '21 at 08:43
  • Yup you don't need to call new String. It is an instance, but it is not modifyable from any calls (all calls to it will create a new modified string automatically). So don't worry about it. You don't need to create a new instance here – IntoVoid Apr 15 '21 at 08:47
  • The existence of `new String(String)` is primarily for historic reasons. Before Java 7 Update 6, when you used `substring(...)` it would share the same underlying character array of the original string, and if you wanted to reduce memory consumption, you had to copy the string so the larger original array could get garbage collected. Java no longer does this, so you (almost) never need to copy the string. You seem to confuse mutability of variable with mutability of objects. – Mark Rotteveel Apr 15 '21 at 08:47
  • I would question why you think you need `new String(orig.value())` in the constructor, but not in the `setValue` method. At least be consistent between the two! (But you don't need `new String(...)`). – Andy Turner Apr 15 '21 at 08:49
  • I don't undestand very well what "immutable" means. If every call creates a new instance, the following code would't work MyClass a = new MyClass(); a.setValue("hello"); a.getValue().concat(" world"); I mean: if "a.getValue()" returns a new copy of the object, where are you asigning the result of "a.getValue().concat(" world")" to a.value inner object? – perere Apr 15 '21 at 09:11
  • Immutable means can't be mutated or changed. If you can't change something, there is no need to take a copy of it, because nobody else can mess up the instance you've already got. If you do take a copy of it, that's not _wrong_; but you just have two (or more) copies of the thing where just one would suffice, so you've used more memory and taken time to construct a thing you didn't need. – Andy Turner Apr 15 '21 at 09:12
  • You only need a single instace. If you do `value = "here"`, then the variable `value` holds a reference to the string object with value "here", when you do `value = "there"`, then `value` now points to a **different** object with the value "there"; the object with value "here" still exists in memory (though it might get garbage collected, though being a string literal as well, that is less likely). – Mark Rotteveel Apr 15 '21 at 09:28

0 Answers0