Classes and Objects in Java: Build and Trace Two Objects Step by Step
Build a Java Student class, create two independent objects, and trace fields, methods, aliases, null references, output, and common errors step by step.
KnowledgeGate Team
Exam prep & CS education

Reading a Java class declaration does not automatically explain what exists after new runs. A class defines available data and operations; each new Student(...) constructs one object with its own field values. Assigning a second reference does not construct another object, and both references can then reach the same state. The Coding & DSA Courses for Placements category places this foundation in a wider learning route.
Classes and objects in Java: the precise mental model
Keep the definition and created data separate:
Term | Meaning in the |
|---|---|
Class |
|
Object or instance | A constructed |
Field | Per-object data, here |
Instance method | Behaviour called through an object, such as |
Constructor | Code that initialises a new object when |
Reference variable | A variable such as |
The class is shared, while each constructed object can hold different state. Methods belong to every Student, but a call operates on the referenced object. Classes and objects answer what state exists and which reference reaches it; encapsulation, inheritance, abstraction, and polymorphism answer later design questions about protecting, specialising, exposing, and dispatching that state. OOP for Teaching CS Exams: Classes and Inheritance places this inside the broader OOP concept set.
Trace creation by counting only new expressions:
After
Student a;, objects created = 0.After
a = new Student("Asha", 10);, objects created = 1.After
Student b = a;, objects created remains 1.After
b = new Student("Bharat", 20);, total objects created = 2.
Student a; declares a reference. It creates zero Student objects.
Java class anatomy: fields, constructor, this, and methods
Our class has private fields String name and int marks. Its constructor has the class name and no return type. In this.name = name, this.name is the current object's field and bare name is the parameter.
addGraceMarks mutates that object's marks, hasPassed computes from its state, getMarks reads it, and printResult prints the values. private blocks unrelated direct changes; methods provide controlled access.

Classes and objects in Java: one fully runnable worked example
Save this complete source as Main.java:
class Student {
private String name;
private int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
void addGraceMarks(int points) {
marks += points;
}
boolean hasPassed() {
return marks >= 40;
}
int getMarks() {
return marks;
}
void printResult() {
System.out.println(name + ": " + marks + ", passed=" + hasPassed());
}
}
public class Main {
public static void main(String[] args) {
Student riya = new Student("Riya", 72);
Student aman = new Student("Aman", 35);
aman.addGraceMarks(8);
riya.printResult();
aman.printResult();
}
}Compile and run it:
javac Main.java
java MainRiya remains at 72, so 72 >= 40 is true. Aman starts at 35 and receives 8 marks: 35 + 8 = 43, and 43 >= 40 is true. The exact output is:
Riya: 72, passed=true
Aman: 43, passed=trueThe call through aman changes Aman from 35 to 43, but Riya stays at 72. The class has no shared marks value; each object has its own. Continue with the Complete Java Course.
Java object references: identity, aliasing, and null
Now extend the live values:
Student selected = aman;
selected.addGraceMarks(2);
System.out.println(aman.getMarks());There is no new, so no object appears. Both references reach the same Aman object. Its mark changes from 43 to 43 + 2 = 45, so the printed value is 45.
By contrast, Student anotherAman = new Student("Aman", 45); creates a different object whose initial field values happen to match. Thus anotherAman == aman is false, while selected == aman is true. Here, == compares reference identity.
With Student waiting = null;, the reference reaches no object. Calling waiting.printResult() therefore throws NullPointerException.

Common Java class and object errors, with direct fixes
Mistake | What goes wrong | Fix |
|---|---|---|
| The local reference is not initialised. | Construct an object before using the reference. |
| It compiles, but the call throws | Ensure the reference points to an object before the call. |
| Only | Pass |
Calling | It is an instance method, not a static method. | Call |
With public class Main, use the filename Main.java. The helper Student can remain non-public in that file. Diagnostic wording varies by JDK, but these causes do not.
For fast debugging, find every new, identify the reference receiving it, write the field values beside that object, and follow each method call through the reference on its left.
How exams and interviews test Java classes and objects
Assessments ask for class-versus-object definitions, object counts that distinguish declarations from new, alias output traces, and constructor or static-context errors. These foundations fit into a broader Technical Interview Prep: OS, DBMS, CN and OOP Answers plan.
A typical mini-trace looks like this:
Student x = new Student("Neha", 38);
Student y = x;
y.addGraceMarks(4);For this mini-trace, the answer is: one object, x.getMarks() prints 42, and x == y is true.
Only one new runs. Both references reach it, and its state becomes 38 + 4 = 42. In an interview: define the class, identify its state, identify its behaviour, show an object with values, then say whether a second variable creates an object or another reference.
Java classes and objects exercises with checkable answers
Write
Bookwith fieldstitleandpages. Constructnew Book("Java Basics", 240)and print exactlyJava Basics has 240 pages.Create
Rectanglewithwidth = 6,height = 4, andarea()returningwidth * height. Then callsetWidth(9)and calculate the area again. Why would a second rectangle retain its own width?Give a
Boxobjectvalue = 12, assignBox second = first, then runsecond.value = 19. PredictSystem.out.println(first.value).
Answers: Exercise 1 has one class, one object, and field values "Java Basics" and 240. Exercise 2 returns 6 * 4 = 24, then 9 * 4 = 36; a separately constructed rectangle keeps independent fields. Exercise 3 prints 19 because both references reach the same object.
Classes and objects in Java: the short version and next step
A class declares a type. new creates an object. A reference gives access to that object. Each independently constructed object has its own instance state. Two references can still point to one shared object. Riya has 72 marks, while the shared Aman object has 45.
Retype the program with Aman starting at 31 and receiving 10 grace marks. Predict 31 + 10 = 41 before running it. If you still need language fundamentals, use the Complete Java Course linked above. If you are ready to apply Java objects while learning data structures, continue with DSA Using Java: Placement Preparation Course.
Now create one class from daily life, construct two objects with different values, mutate only one, and trace the exact output by hand before compiling.
Keep learning

Packages and Access Modifiers in Java: Visibility Rules with Runnable Examples
Learn how Java package boundaries change member visibility. Compile a complete example covering public, protected, package-private and private access.

OOP Basics in Java: Classes, Objects and the Four Pillars with Runnable Examples
Connect Java's OOP syntax to design decisions with a complete Account example. Trace two objects through validation, overriding and runtime dispatch to exact output.

Abstraction and Interfaces in Java: Abstract Classes, Contracts and Runnable Examples
Learn when Java needs an abstract class, when it needs an interface, and how both work together in one runnable charge-calculation program with exact outputs.

Wrapper Classes and Autoboxing in Java: Worked Examples and Null Traps
Learn why Java wrapper classes exist, how boxing and unboxing work, and where nulls, reference identity, and overload selection create surprises.