Java classes describe a type; objects are individual values created from that type. Understanding this distinction makes constructors, fields and method calls much easier to read. This lesson follows three small programs that separate independent objects, shared references and identity comparisons.
Review the related foundation lesson when you need a prerequisite. The complete programs below use Java 17-compatible syntax.
Classes versus instances
A class declares a type with fields and methods. An object is a particular instance of that class, with its own instance state.
Think of a class declaration as a description that Java can check, rather than as an object waiting to be filled. Declaring Member member does not create a member. The variable can hold a reference, and new Member(...) creates the instance it will refer to. A method parameter typed Member can receive any suitable member object; it does not secretly create another member when a call begins.
Object creation and references
new creates an object and invokes a constructor. A reference variable points to that object; copying the reference does not copy the object itself.
Each evaluation of new normally creates a distinct instance. Assigning a reference copies the reference value, so two variables can name the same mutable object. Java passes that reference value by value when calling a method. Reassigning the parameter cannot redirect the caller's variable, but changing the object's field through the copied reference changes the shared object. This explains many surprises that are incorrectly blamed on constructors.
Class responsibility and object equality
Keep a class responsible for one coherent concept. A member record should represent member information, while a separate service can handle printing, storage or notifications.
Object identity and meaningful equality answer different questions. The == operator compares whether two references designate the same object. A class can override equals to compare chosen contents, but the default Object implementation uses identity. Two freshly created Member objects can have identical names and still be distinct. Design the equality rule deliberately, especially before using objects as map keys or set elements.
Running the examples
Save name.java, compile it with javac --release 17 name.java, and run java name. Use each program's own public class name and filename.
How MemberObjects produces its output
The Member constructor stores the supplied name and borrowed count in the newly created member. main creates Sol with three borrowed items, then reads those fields through the member reference. The colon belongs to the output formatting, not to the object. No second member is created during printing, and the field values remain unchanged after the call.
How IndependentMembers produces its output
Both new expressions create a Card, and both visits fields begin at zero because instance numeric fields have default values. Assigning four through first changes only that first card. Reading second.visits still produces zero. This result would change if visits were static, because the two expressions would then access one class field rather than separate instance fields.
How MemberAliases produces its output
The assignment to alias copies the reference stored in original. It does not run new and does not duplicate the Card. Therefore alias.visits and original.visits reach the same field, which contains seven after the update. The identity comparison returns true because both reference values designate that one card. A fresh Card would instead compare false.
Object lifetime and null references
A reference can be null, meaning it currently refers to no object. Calling an instance method through null throws NullPointerException; it does not create a missing instance automatically. Assigning null to one variable also does not destroy an object while another usable reference still reaches it. Java's garbage collector can reclaim unreachable objects, but you cannot predict an exact collection moment from an assignment. For a small practice model, create two member objects and one alias. Update the alias's borrowed count, then compare all three references. Explain which members share state and which object remains independent before adding another method.
Java Summary
- A class declares a type with fields and methods. An object is a particular instance of that class, with its own instance state.
- new creates an object and invokes a constructor. A reference variable points to that object; copying the reference does not copy the object itself.
- Instance fields describe each object separately. Static fields belong to the class and are shared by its instances.
- Keep a class responsible for one coherent concept. A member record should represent member information, while a separate service can handle printing, storage or notifications.
- Continue with Java exercises, Java quizzes and the Java study roadmap.
Java Examples
Each example shows the code, the output it produces and what each part does.
Java Classes and Objects Explained in a complete program
public class MemberObjects {
static class Member {
String name;
int borrowed;
Member(String name, int borrowed) {
this.name = name;
this.borrowed = borrowed;
}
}
public static void main(String[] args) {
Member member = new Member("Sol", 3);
System.out.println(member.name + ": " + member.borrowed);
}
}Sol: 3Line-by-line explanation
- Line 12Evaluates
member.name + ": " + member.borrowedand printsSol: 3.
Two objects keep separate state
public class IndependentMembers {
static class Card {
int visits;
}
public static void main(String[] args) {
Card first = new Card();
Card second = new Card();
first.visits = 4;
System.out.println(first.visits + ":" + second.visits);
}
}4:0Line-by-line explanation
- Line 9Evaluates
first.visits + ":" + second.visitsand prints4:0.
Copying a reference creates an alias
public class MemberAliases {
static class Card {
int visits;
}
public static void main(String[] args) {
Card original = new Card();
Card alias = original;
alias.visits = 7;
System.out.println(original.visits);
System.out.println(original == alias);
}
}7
trueLine-by-line explanation
- Line 9Evaluates
original.visitsand prints7. - Line 10Evaluates
original == aliasand printstrue.
Java FAQ
Does declaring a class create its objects?
No. The declaration defines a type and its members. Objects are created separately, often with new and a constructor call. A program can declare a useful class without constructing any instances during a particular run.
Are Java objects passed by reference?
Java passes arguments by value, including reference values. A called method receives its own parameter variable containing the copied reference. It can mutate a shared object's accessible state, but assigning a different object to that parameter does not reassign the caller's variable.
What does new actually do?
new asks the JVM for memory for a fresh object, sets every field to its default value, runs any field initialisers and the chosen constructor, and finally returns a reference to the object. That reference is what you store in a variable; the object itself lives on the heap.
When does Java delete an object?
Java has no delete and no destructors. When no reachable reference points to an object any more, the garbage collector may reclaim its memory at some later time. Resources such as files and connections shouldn't wait for that; close them explicitly, usually with try-with-resources.
What is the difference between == and equals() for objects?
== checks identity: whether two references point to the same object. equals() checks whether two objects should count as equal according to the class's rules. A class that doesn't override equals() inherits the identity version from Object, so two separately created objects with the same values are not equal until you override it, or use a record.
Java Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Two variables are assigned the same Card reference. Which operation changes the shared card?
What does == compare for two non-null object references?
Java Quiz
2 questions to check what you learned.
Why should a member's borrowed count usually be an instance field?
- Each member needs an independent count
- An instance field can never change
- Static fields cannot store integers
Two new Card objects have visits equal to zero. Are they identical objects?
- No, they are distinct instances
- Yes, equal field values make identity equal
- Only after their fields are printed