Trouble understanding Object State, Behavior, and Identity?

All objects have three essential features:

  • state
  • behavior
  • identity

An object's state is defined by the attributes of the object and by the values these have. In your case, name, age, gender

The term "behavior" refers to how objects interact with each other, and it is defined by the operations an object can perform. In your case, student object, can do some activity?

student.getResults();
  • Identity:

Even objects with the same properties and behavior have their own individual identity.

For Example,

2 blue station wagons that were built in the same year by the same manufacturer are still separate and unique cars.

The identity of an object is independent of its attributes or operations. So an object will retain its identity no matter what values its properties have.


Characteristics of objects are:

State: what the objects have, Student have a first name, last name, age, etc

Behavior: what the objects do, Student attend a course "Java for beginners"

Identity: what makes them unique, Student have Student-ID-number, or an email which is unique. (this is important when implementing the equals method, to determine if the objects are different or not)

Student john = new Student("John");
john.setCurrentActivity("Learning Java");
john.setAge(21);
john.setWeight(173);
john.setAddress(...);
john.setHobbies(...);

and you can figure out the getters.

public class Student {
    private String name;
    private int    age;
    //etc

    // construct a new student 
    public Student(String name) {
        this.name   = name;
    }

    public setAge(int age) {
        this.age   = age;
    }

    public int getAge() {
        return age;
    }
}

An illustration of a Car object, which I found that might help you some...

Car state:

  • Speed
  • RPM
  • Gear
  • Direction
  • Fuel level
  • Engine temperature

Behaviors:

  • Change Gear
  • Go faster/slower
  • Go in reverse
  • Stop
  • Shut-off

Identity:

  • VIN
  • License Plate

As a correction to @MrSimpleMind's answer marked as best answer, and elaborating on @JNL's answer:

Identity is not what makes the object unique in terms of it's state (e.g. name = "Tim" or whatever). Identity however is that an object is unique in terms of it's location in memory.
If you what to read more about this, you can start by looking at this Wiki page: Identity in OOP

UPDATE
It's worth mentioning that it's not always in terms of location in memory. When saving an object to a database, it'll essentially be saved as a row, here an ID column is used.