Setting objects equal to eachother (java)

So I have a class called Person which looks like

public class Person {

    private String personName;


    public String toString(){
        return personName;
    }

    public Person(String personName){
        this.personName = personName;
    }

}

      

and another class in which I create a human object

public class IdanJavaTask {

    public static void main(String[] args) {

        Person p1 = new Person("Bob");
        System.out.println("p1 : " + p1);

        Person p2 = new Person("Joe");
        System.out.println("p2 :" + p2);

    }

}

      

so far everything is fine and my print operator is p1: Bob
p2: Joe

Now I want to create a new object, p3 and set it to p1 my class now looks like this:

public class IdanJavaTask {

    public static void main(String[] args) {

        Person p1 = new Person("Bob");
        System.out.println("p1 : " + p1);

        Person p2 = new Person("Joe");
        System.out.println("p2 :" + p2);

        Person p3 = new Person (p1);
        System.out.println("p3 equal to p1:" + p3.equals(p1));

    }

}

      

when i try to do this i get the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor Person(Person) is undefined

    at vehicleassignment.IdanJavaTask.main(IdanJavaTask.java:13)

      

I think I will need to add a method to my main class (Person), but I don't know why or what to add? why can't I just set the objects equal to each other?

+3


source to share


4 answers


There are two ways to interpret "set objects equal to each other".

First, you want to p1

, and p3

referred to the same object. Like Clark Kent and Superman, these are two names (links) for the same person. This is accomplished by:

Person p1 = new Person("Jim");
Person p3 = p1;

      

In this case, if something happens to p1

, it happened to p3

. If you kill Clark Kent, you killed Superman (since they are the same thing). Java defines equality with a method equals(Object o)

- two objects a

and b

are equal iff a.equals(b)

and b.equals(a)

return true

. These two objects will be equal using the basic definition of Object

equality, so you don't need to worry about that.

Another way to interpret your meaning is to create a new human object, which turns out to be an exact copy of the first person. To do this, you need to add another constructor to your person class that takes a person as an argument:

public class Person {

    private String personName;

    public String toString(){
        return personName;
    }

    public Person(String personName){
        this.personName = personName;
    }

    public Person(Person personToCopy){
        this.personName = personToCopy.personName;
    }

}

      

With this setting, you can do what you do in your main.



Person p1 = new Person("Bob");
Person p3 = new Person(p1); //Will have name Bob.

      

To make p1

both p3

equal, we must teach the Person class to use its fields to test for equality. We can do this by overriding the method equals

in the class.

public boolean equals(Object o){
    if(! (o instanceof Person)) return false; //a Person can't be equal to a non-person

    Person p = (Person) o;
    return personName == null && p.personName == null || personName.equals(p.personName);
}

      

Whenever we overwrite a method equals

, it's a good idea to also overwrite the method hashcode

that returns unique int

for each object. Since the only field the object has Person

is its name, we can just use this hashcode.

public int hashCode(){
    return personName == null ? 0 : personName.hashCode();
}

      

So our entire Person class looks like this:

public class Person {

    private String personName;

    public String toString(){
        return personName;
    }

    public Person(String personName){
        this.personName = personName;
    }

    public Person(Person personToCopy){
        this.personName = personToCopy.personName;
    }

    public boolean equals(Object o){
        if(! (o instanceof Person)) return false; //a Person can't be equal to a non-person

        Person p = (Person) o;
        return personName == null && p.personName == null || personName.equals(p.personName);
    }

    public int hashCode(){
        return personName == null ? 0 : personName.hashCode();
    }
}

      

+7


source


Person p3 = new Person(p1);

      

This is called a copy constructor. You must define it explicitly, in this case:

public Person(Person p) {
    this.personName = p.personName;
}

      



You also need to override the method equals()

(and method hashCode()

) to use it, otherwise the equals()

root class method will be used Object

which always returns false:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Person other = (Person) obj;
    if (personName == null) {
        if (other.personName != null)
            return false;
    } else if (!personName.equals(other.personName))
        return false;
    return true;
}

      

See What issues to consider when overriding equals and hashCode in Java? ...

+3


source


No object in Java has a default copy constructor, except for autoboxed objects like Float

or Integer

which are actually copied.

This means that in all circumstances, it is your responsibility to define a copy constructor and specify what is copied in your example:

public Person(Person other) {
  this.personName = other.personName;
}

      

Since in Java everything is passed by reference, a simple assignment just makes 2 variables point to the same instance, like so:

Person p1 = new Person("Joe");
Person p2 = p1;
// now both point to the same object, not intended behavior in your case

      

+1


source


If you want p3 to refer to p2, that means changing to p2 will update p3 and vice versa, just do

Person p3 = p2;

      

if you want to clone data and have two different but "equal" copies of a person, you can implement a constructor in Person that takes a Person and copies the values ​​into the class fields.

0


source







All Articles