0

Given the next example:

//class
public class Person {
    private int id;
    private String name;
    private int age;

    //class constructor to instantiate Person class
    public Person (int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }

    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 (id != other.id)
            return false;
        return true;
    }

    public static void main(String[] args) {
        //case 1
        Person p1 =  new Person(1, "Carlos", 20);
        Person p2 = new Person(1, "Carlos", 20);

        //case 2
        Person p3 = new Person(2, "John", 15);
        Person p4 = new Person(3, "Mary", 17);
    }
}

Can I conclude the following?:

  1. For case 1, I have two instances of the class Person, but both instances represent the same object.
  2. For case 2, I have two instances of the class Person, but each instance represent a different object.

In other words, instance and object have a different semantically meaning? today both terms are treated as synonyms, But I am not so sure if they are really the same thing (personally, I think they are different things).

Honestly, I feel more comfortable with Alfred blog definitions:

Object: real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). Car have state (current speed, current gear) and state (applying brake, changing gear). Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields and exposes its behavior through methods.

Class: is a “template” / “blueprint” that is used to create objects. Basically, a class will consists of field, static field, method, static method and constructor. Field is used to hold the state of the class (eg: name of Student object). Method is used to represent the behavior of the class (eg: how a Student object going to stand-up). Constructor is used to create a new Instance of the Class.

Instance: An instance is a unique copy of a Class that representing an Object. When a new instance of a class is created, the JVM will allocate a room of memory for that class instance.

Carlos Casallas
  • 304
  • 3
  • 9
  • 1
    In case 1 I'd say that you have 2 instances of class `Person` which _might_ represent the same (realworld) person - or they represent 2 different persons which happen to have the same name and age. Whether they can be considered to be the same person would depend on what data you use to identify them - e.g. something like passport id, finger print etc. Since there's no information on that in the code you posted it's hard to say. – Thomas Jan 31 '18 at 15:27
  • In both cases, you have two unequal objects (since you haven't defined `.equals` and `.hashcode`) and two instances. – bcsb1001 Jan 31 '18 at 15:28
  • @Thomas If I added an id attribute an implements equals and hashcode based on it and for case 1 set the same id for both instances, then both intances represent the same object? I am going to edit the question – Carlos Casallas Jan 31 '18 at 15:44
  • Case 1.- You have 2 instances of Person class who represent the same Object (you've overriden equals and hashcode method using only id attribute, and they have the same), placed in different memory regions. In case 2 you have 2 instances of Person class who represent 2 different objects as they have different ids. – Francisco Valle Jan 31 '18 at 16:12
  • @FranciscoValle yes, I understand it in the same way, therefore I am confirming that instance and object are not synonyms, what goes against of the suggested answer in https://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances – Carlos Casallas Jan 31 '18 at 16:22
  • Class is somre real object logical representation, written in code (your person class). An instance is one particular representation of the class, placed into memory. An object is what a class instance represents. In your example a Person is a class that describe one real world object (an human with identifier, name and age). The class is the representation and can be extended (with sex, ...). Even if the instances represents same object (same id), they are not in asme memory region, so to update the object 1 you need to search all instances of the object with id 1 and update them. – Francisco Valle Jan 31 '18 at 17:12

0 Answers0