Java Classes and Objects
›Short versionOpen if you only have a minute.
- A class is the type: fields, methods, and constructors. An object is one instance created with new.
- A variable holds a reference. Copying the variable copies the reference, so both names see the same object.
- == compares identity. equals should compare the state that matters, and hashCode must agree with equals.
- Each object has its own instance fields. A static field belongs to the class and is shared.
- The constructor runs during new and should leave the object valid before any caller uses it.
- A public class must live in a file of the same name. Book belongs in Book.java.
Two readers finish the same novel. Same title, same author, two copies on two tables. If one reader writes a note in the margin, the other copy stays clean. A Java is the title page that says what a book is. Each is one copy, with its own margin.
The class, then one copy
Book declares a for the title and a method that reads it. Nothing here is a particular book yet. The class is the description the compiler and the runtime share.
public class Book {
private final String title;
public Book(String title) {
if (title == null || title.isBlank()) {
throw new IllegalArgumentException("title");
}
this.title = title;
}
public String title() {
return title;
}
}new Book("Dune") allocates the object and runs the . The variable dune stores a . The title lives inside the object, not inside the variable.
Book dune = new Book("Dune");
System.out.println(dune.title());$ javac Book.java Main.java$ java Main
Two names, one object
Assignment between reference variables copies the pointer. After Book alias = dune, both variables refer to the single object that new created. is that fact: there is one object, however many names point at it.
Book dune = new Book("Dune");
Book alias = dune;
Book another = new Book("Dune");
System.out.println(dune == alias); // true, same object
System.out.println(dune == another); // false, two objects== answers "same object?". It stays false for dune and another even though both titles are the word Dune. Value comparison belongs in equals. The matching hashCode must use the same fields, because hash-based collections place an object by hash code and then confirm with equals. Two books that compare equal and hash differently will disappear from a HashSet.
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Book book)) {
return false;
}
return title.equals(book.title);
}
@Override
public int hashCode() {
return title.hashCode();
}Where the class lives
A public class is visible outside its package, and the source file must be named after it. public class Book belongs in Book.java. A second class in that file is allowed only if it is not public. Most of the time, one public class per file keeps the name you import and the name you compile as the same word.
Instance fields are per object. If Book also had private int marginNotes, marking one copy would leave the other at zero. A static field would be shared, which is the wrong place for a margin that belongs to one reader's copy.
Book a = new Book("Dune"); Book b = a; Book c = new Book("Dune"); Which pairs are the same object, and which compare equal if equals uses the title?
a == b is true. b is a second reference to the object a already names.a == c is false because c came from a second new. If equals compares titles, a.equals(c) is true, and hashCode must return the same value for both.