Classes & Objects in Java
Java Master Course — Chapter 12 of 50
This chapter goes deeper into the two most important building blocks of Java OOP:
Classes and Objects
Chapter 11 introduced the basic OOP idea. Here we will understand exactly how classes are written, how objects are created, how fields and methods work, how references work, how multiple objects maintain separate state, and how objects interact with methods.
Constructors,
this,static, inheritance, polymorphism, and encapsulation are covered in later chapters.
1. What You Will Learn#
By the end of this chapter, you should understand:
- Class syntax
- Object creation
- new keyword
- Fields
- Instance fields
- Methods inside classes
- Object references
- Object identity
- Multiple objects
- Separate object state
- Shared references
- null references
- Field initialization
- Default field values
- Instance methods
- Method calls through objects
- Object parameters
- Returning objects
- Arrays of objects
- Objects containing objects
- Basic memory/reference model
- Object lifecycle basics
- Object equality basics
- Common object mistakes
- Practical OOP programs
- Exercises
- Interview questions
2. Quick Recap of Chapter 11#
OOP organizes software around objects.
An object can have:
state
+
behavior
State is usually represented using fields.
Behavior is usually represented using methods.
A class defines a type that can be used to create objects.
Example:
class Student {
String name;
int age;
void study() {
System.out.println(name + " is studying.");
}
}
Object creation:
Student student = new Student();
3. Class vs Object#
Remember this distinction:
CLASS
↓
definition/type
OBJECT
↓
instance
Example:
class Car {
String brand;
}
Car is the class/type.
Then:
Car car = new Car();
creates an object.
4. What Is a Class?#
A class is a Java type definition.
A class can contain members such as:
fields
methods
constructors
nested classes/interfaces/enums
Example:
class Student {
String name;
int age;
void study() {
System.out.println("Studying");
}
}
5. Class as a Blueprint#
A common analogy is:
class = blueprint
object = actual thing made using the blueprint
For example:
Car class
↓
┌───┼────┐
↓ ↓ ↓
Car1 Car2 Car3
The class describes the common structure.
Each object has its own instance state.
6. Class Is More Than a Blueprint#
The blueprint analogy is useful, but incomplete.
A class is also:
a type
When you write:
class Student {
}
you create a new reference type:
Student
Now Java can declare:
Student s;
7. Basic Class Syntax#
class ClassName {
// fields
// methods
}
Example:
class Student {
String name;
int age;
void study() {
System.out.println("Studying");
}
}
8. Class Naming Convention#
Java convention uses:
PascalCase
for class names.
Good:
Student
BankAccount
Car
Employee
ShoppingCart
Avoid:
student
bankaccount
my_class
unless there is a specific reason.
9. Fields#
A field is a variable declared as a member of a class.
Example:
class Student {
String name;
int age;
}
Here:
name
age
are fields.
10. Instance Fields#
If a field is not declared static, it is normally an instance field.
Example:
class Student {
String name;
int age;
}
Every Student object has its own instance state for:
name
age
11. Creating an Object#
Use:
new
Example:
Student student = new Student();
This statement has multiple parts.
Student
↓
reference type
student
↓
reference variable
new Student()
↓
creates Student object
12. Breaking Down Object Creation#
Consider:
Student student = new Student();
Left side:
Student student
declares a variable named student that can hold a reference to a Student object.
Right side:
new Student()
creates a new Student object.
The resulting reference is assigned to:
student
13. Does Declaration Create an Object?#
No.
This:
Student student;
only declares a reference variable.
It does not create a Student object.
14. Object Creation#
This creates an object:
Student student = new Student();
Or in two steps:
Student student;
student = new Student();
15. Object Creation with new#
General form:
Type reference = new Type();
Examples:
Car car = new Car();
Book book = new Book();
Employee employee = new Employee();
BankAccount account = new BankAccount();
16. The new Keyword#
new is used to create new instances of classes, arrays, and other applicable reference types.
Example:
Student s = new Student();
The expression:
new Student()
creates an object and produces a reference to it.
17. Object Reference#
The variable:
student
does not contain the entire Student object in the normal conceptual model.
It contains a reference value that identifies the object.
Think:
student ───────► Student object
This distinction is extremely important.
18. Reference Diagram#
Code:
Student student = new Student();
Conceptually:
student
│
▼
┌──────────────┐
│ Student │
│ name = null │
│ age = 0 │
└──────────────┘
The exact JVM memory implementation is more complex, but this reference model is useful for learning.
19. Object Memory — Important Note#
You may hear:
objects are always on heap
variables are always on stack
This is an oversimplification.
The Java language specification does not define the JVM memory model using such a simplistic universal rule.
For learning, it is useful to think:
reference variable
↓
reference
↓
object
and later study actual JVM memory behavior separately.
20. Instance State#
Example:
class Student {
String name;
int age;
}
Object:
Student s = new Student();
Initially, the fields have their default values:
name → null
age → 0
Then:
s.name = "Aman";
s.age = 20;
The object's state becomes:
name = Aman
age = 20
21. Reading Fields#
Use the dot operator:
.
Example:
System.out.println(s.name);
System.out.println(s.age);
22. Updating Fields#
Example:
s.name = "Riya";
s.age = 21;
This changes the state of the object referenced by s.
23. Field Access Syntax#
General form:
reference.field
Examples:
student.name
student.age
car.speed
account.balance
Access is subject to Java's access-control rules.
24. Methods Inside a Class#
Example:
class Student {
String name;
void study() {
System.out.println(name + " is studying.");
}
}
The method:
study()
is defined inside the Student class.
25. Calling an Instance Method#
Create an object:
Student student = new Student();
Set state:
student.name = "Aman";
Call method:
student.study();
Output:
Aman is studying.
26. Instance Method Concept#
An instance method can operate on the particular object through which it is invoked.
Example:
class Car {
int speed;
void accelerate() {
speed += 10;
}
}
Usage:
Car car = new Car();
car.accelerate();
The method changes that Car object's state.
27. Multiple Objects#
One class can create many objects.
Example:
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Conceptually:
s1 ───► Student object 1
s2 ───► Student object 2
s3 ───► Student object 3
28. Separate State#
Suppose:
class Student {
String name;
}
Then:
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Aman";
s2.name = "Riya";
Output:
System.out.println(s1.name);
System.out.println(s2.name);
Aman
Riya
Each object has its own name field.
29. Object State Is Independent#
If:
s1.name = "Changed";
it does not normally change:
s2.name
because s1 and s2 refer to separate objects.
30. Multiple Object Diagram#
s1 ───► ┌────────────────┐
│ Student │
│ name = Aman │
└────────────────┘
s2 ───► ┌────────────────┐
│ Student │
│ name = Riya │
└────────────────┘
One class.
Two objects.
Separate instance state.
31. Object Identity#
Object identity means that one object instance is distinguishable from another.
Example:
Student s1 = new Student();
Student s2 = new Student();
Even if:
s1.name = "Aman";
s2.name = "Aman";
they are still two separate object instances.
32. Same State Does Not Mean Same Object#
Example:
Object A:
name = Aman
age = 20
Object B:
name = Aman
age = 20
They may have identical state.
But they are still different objects.
33. Reference Equality with ==#
For reference values:
s1 == s2
checks whether both references identify the same object.
Example:
Student s1 = new Student();
Student s2 = new Student();
System.out.println(s1 == s2);
Output:
false
34. Same Reference#
Example:
Student s1 = new Student();
Student s2 = s1;
Now:
s1 == s2
is:
true
because both references identify the same object.
35. Shared Reference Diagram#
s1 ─────┐
│
▼
┌──────────────┐
│ Student │
│ name = null │
└──────────────┘
▲
│
s2 ─────┘
One object.
Two references.
36. Changing Through One Reference#
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = s1;
s1.name = "Aman";
System.out.println(s2.name);
}
}
Output:
Aman
Why?
Because s1 and s2 refer to the same object.
37. Reassigning One Reference#
Consider:
Student s1 = new Student();
Student s2 = s1;
s1 = new Student();
Now:
s1 ───► Object B
s2 ───► Object A
Changing what s1 refers to does not change s2.
38. null Reference#
A reference can contain:
null
Example:
Student student = null;
This means:
student currently refers to no object
39. Null Diagram#
student
│
▼
null
There is no Student object at the end of the reference.
40. NullPointerException#
Example:
Student student = null;
System.out.println(student.name);
This causes:
NullPointerException
because Java cannot access an instance field through a null reference.
41. Fixing Null Reference#
Create an object:
Student student = new Student();
System.out.println(student.name);
Now the reference identifies a Student object.
42. Null Check#
You can check:
if (student != null) {
System.out.println(student.name);
}
This avoids dereferencing the reference when it is null.
43. Fields Have Default Values#
Example:
class Student {
String name;
int age;
double marks;
boolean active;
char grade;
}
For a newly initialized object, fields get default values according to their types.
Conceptually:
name → null
age → 0
marks → 0.0
active → false
grade → '\u0000'
44. Reference Field Default#
Example:
class Student {
String name;
}
After:
Student s = new Student();
the name field is initially:
null
unless another initialization supplies a different value.
45. Primitive Field Defaults#
Common defaults:
byte → 0
short → 0
int → 0
long → 0L
float → 0.0f
double → 0.0d
char → '\u0000'
boolean → false
46. Field Initializers#
You can give fields initial values.
Example:
class Student {
String name = "Unknown";
int age = 18;
}
Then:
Student s = new Student();
starts with:
name = Unknown
age = 18
47. Multiple Object Initialization#
Each object gets its own instance field values.
class Counter {
int value = 10;
}
Then:
Counter a = new Counter();
Counter b = new Counter();
a.value = 50;
Now:
a.value = 50
b.value = 10
48. Instance Field vs Local Variable#
Example:
class Student {
int age;
void show() {
int marks;
}
}
Here:
age
is an instance field.
marks
is a local variable.
Fields get default values.
Local variables do not automatically get usable default values.
49. Local Variable Example#
This is invalid:
void test() {
int x;
System.out.println(x);
}
Java reports that x might not have been initialized.
Correct:
void test() {
int x = 10;
System.out.println(x);
}
50. Field Access from an Instance Method#
Example:
class Student {
String name;
int marks;
void show() {
System.out.println(name);
System.out.println(marks);
}
}
Inside an instance method, you can directly access instance fields of the current object.
51. Using this Explicitly#
The same code can be written:
class Student {
String name;
int marks;
void show() {
System.out.println(this.name);
System.out.println(this.marks);
}
}
this refers to the current object.
Detailed this usage is covered in Chapter 14.
52. Method Call and Current Object#
Suppose:
Student s1 = new Student();
Student s2 = new Student();
s1.show();
s2.show();
The method is the same method defined in the class.
But each invocation operates in the context of the object used for the call.
Conceptually:
s1.show()
→ this refers to s1
s2.show()
→ this refers to s2
53. Object Methods Can Return Values#
Example:
class Rectangle {
double length;
double width;
double area() {
return length * width;
}
}
Usage:
Rectangle r = new Rectangle();
r.length = 10;
r.width = 5;
double result = r.area();
System.out.println(result);
Output:
50.0
54. Object Methods Can Accept Parameters#
Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
}
Usage:
Calculator calculator = new Calculator();
System.out.println(calculator.add(10, 20));
Output:
30
55. Methods Can Modify Object State#
Example:
class Counter {
int value;
void increment() {
value++;
}
}
Usage:
Counter counter = new Counter();
counter.increment();
counter.increment();
System.out.println(counter.value);
Output:
2
56. State-Changing Method#
A method that changes object state is often called a mutating method.
Example:
void increment() {
value++;
}
The object's state changes.
57. Non-Mutating Method#
A method can simply calculate or read information.
Example:
double area() {
return length * width;
}
This method does not need to modify the rectangle's fields.
58. Method Parameters That Are Objects#
A method can accept an object reference.
Example:
class Student {
String name;
}
class Printer {
void print(Student student) {
System.out.println(student.name);
}
}
Usage:
Student student = new Student();
student.name = "Aman";
Printer printer = new Printer();
printer.print(student);
Output:
Aman
59. Object Arguments Are Passed by Value#
Java is always pass-by-value.
For an object parameter, the copied value is the reference.
Example:
static void changeName(Student student) {
student.name = "Riya";
}
Calling:
Student s = new Student();
s.name = "Aman";
changeName(s);
allows the method to modify the same object.
60. Why Does Object Modification Work?#
Conceptually:
caller reference
│
▼
Object A
method parameter
│
▼
same Object A
The reference value is copied.
Both references identify the same object.
61. Parameter Reassignment Is Different#
Example:
static void replace(Student student) {
student = new Student();
}
This changes the local parameter reference.
It does not make the caller's reference point to the new object.
62. Example of Parameter Reassignment#
class Student {
String name;
}
public class Main {
static void replace(Student student) {
student = new Student();
student.name = "Riya";
}
public static void main(String[] args) {
Student s = new Student();
s.name = "Aman";
replace(s);
System.out.println(s.name);
}
}
Output:
Aman
63. Returning an Object#
A method can return a reference to an object.
Example:
class Student {
String name;
}
class StudentFactory {
static Student createStudent() {
Student student = new Student();
student.name = "Aman";
return student;
}
}
Usage:
Student s = StudentFactory.createStudent();
System.out.println(s.name);
Output:
Aman
64. Object Factory Idea#
A factory method is a method that creates and returns an object.
Example:
static Student createStudent(String name) {
Student student = new Student();
student.name = name;
return student;
}
Usage:
Student a = createStudent("Aman");
Student b = createStudent("Riya");
Constructors will provide a better initialization mechanism later.
65. Returning null#
A method returning a reference type can return null.
Example:
static Student findStudent() {
return null;
}
Caller:
Student student = findStudent();
The caller must handle the possibility of null when appropriate.
66. Objects Inside Objects#
A class can have another object as a field.
Example:
class Address {
String city;
}
class Student {
String name;
Address address;
}
Now:
Student HAS-A Address
67. Creating Nested Object State#
Student student = new Student();
student.name = "Aman";
student.address = new Address();
student.address.city = "Mumbai";
Conceptually:
student
│
▼
Student object
│
├── name = Aman
│
└── address ───► Address object
│
└── city = Mumbai
68. Why Object Composition Matters#
Large applications are built by combining smaller objects.
Examples:
Car → Engine
Student → Address
Order → Customer
Order → Product
Computer → Processor
This is called composition or object collaboration depending on the exact relationship.
Detailed OOP relationships are covered in Chapter 22.
69. Null Object Field#
Consider:
class Student {
Address address;
}
A newly created Student has:
address = null
until an Address object is assigned.
So:
student.address.city
can fail if student.address is null.
70. Safe Initialization#
You can initialize the field:
class Student {
Address address = new Address();
}
or initialize it through a constructor later.
The correct design depends on whether every Student should always have an Address object.
71. Arrays of Objects#
Arrays can store references to objects.
Example:
Student[] students = new Student[3];
Important:
This creates the array.
It does not create three Student objects.
72. Object Array Initialization#
You must create each object:
Student[] students = new Student[3];
students[0] = new Student();
students[1] = new Student();
students[2] = new Student();
Now all three elements refer to Student objects.
73. Object Array Diagram#
students
│
▼
┌──────┬──────┬──────┐
│ 0 │ 1 │ 2 │
└──┬───┴──┬───┴──┬───┘
│ │ │
▼ ▼ ▼
Student Student Student
74. Object Array Contains References#
This:
Student[] students = new Student[3];
creates an array whose elements initially contain:
null
null
null
It does not create:
Student
Student
Student
objects automatically.
75. Accessing Object Array#
students[0].name = "Aman";
students[1].name = "Riya";
students[2].name = "Raj";
Then:
System.out.println(students[1].name);
Output:
Riya
76. Common Object Array Error#
This is dangerous:
Student[] students = new Student[3];
students[0].name = "Aman";
The array element:
students[0]
is still null.
Therefore accessing:
students[0].name
causes:
NullPointerException
77. Correct Object Array Code#
Student[] students = new Student[3];
for (int i = 0; i < students.length; i++) {
students[i] = new Student();
}
Then:
students[0].name = "Aman";
is valid.
78. Passing Object Arrays to Methods#
Example:
static void printStudents(Student[] students) {
for (Student student : students) {
System.out.println(student.name);
}
}
Usage:
printStudents(students);
The array reference is passed by value.
The method can access the same array object.
79. Returning an Object Array#
Example:
static Student[] createStudents() {
Student[] students = new Student[2];
students[0] = new Student();
students[1] = new Student();
students[0].name = "Aman";
students[1].name = "Riya";
return students;
}
Usage:
Student[] students = createStudents();
80. Objects and Arrays Together#
A real application may contain:
Array
↓
references
↓
objects
↓
fields
↓
other objects
For example:
Student[]
↓
Student
↓
Address
This is how complex object graphs are formed.
81. Object Graph#
An object graph is a network of objects connected through references.
Example:
Customer
│
├── Address
│
└── Order
│
├── Product
└── Product
This is a useful way to visualize real applications.
82. Reference Chains#
Consider:
order.customer.address.city
There are multiple references:
order
↓
customer
↓
address
↓
city
Any intermediate reference can potentially be null.
83. Avoiding Long Null Chains#
Instead of exposing a deeply nested structure everywhere, good class design can provide appropriate methods.
For example:
order.getCustomerCity()
or another well-designed API.
This is not a strict rule; the design depends on the domain.
84. Object State and Mutable Fields#
Suppose:
class Account {
double balance;
}
Then:
account.balance = 1000;
account.balance = 500;
changes the object's state.
This object is mutable because its state can change.
85. Mutable Object Example#
class Counter {
int value;
void increment() {
value++;
}
}
Every call:
counter.increment();
changes the object.
86. Immutable Object Preview#
An immutable object does not expose operations that change its state after creation.
Java's:
String
is an important example.
Immutable object design will be discussed later.
87. Instance Method vs Static Method#
Instance method:
class Student {
String name;
void showName() {
System.out.println(name);
}
}
Call:
Student s = new Student();
s.showName();
Static method:
class MathUtil {
static int add(int a, int b) {
return a + b;
}
}
Call:
MathUtil.add(10, 20);
Static behavior is covered deeply in Chapter 14.
88. Why Instance Methods Need an Object#
Consider:
class Student {
String name;
void showName() {
System.out.println(name);
}
}
Which name should the method print?
It depends on the object:
s1.showName();
s2.showName();
Each call has a different current object.
89. Object as Context#
Think of:
s1.showName();
as:
run showName()
for object s1
and:
s2.showName();
as:
run showName()
for object s2
This is the key idea behind instance methods.
90. Class with Multiple Fields and Methods#
Example:
class Employee {
String name;
double salary;
void work() {
System.out.println(name + " is working.");
}
double annualSalary() {
return salary * 12;
}
void showDetails() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
91. Using Employee Objects#
Employee e1 = new Employee();
e1.name = "Aman";
e1.salary = 50000;
e1.work();
System.out.println(e1.annualSalary());
e1.showDetails();
Output:
Aman is working.
600000.0
Name: Aman
Salary: 50000.0
92. Multiple Employee Objects#
Employee e1 = new Employee();
Employee e2 = new Employee();
e1.name = "Aman";
e1.salary = 50000;
e2.name = "Riya";
e2.salary = 60000;
Now:
e1 → Aman, 50000
e2 → Riya, 60000
93. Object Method Can Call Another Method#
Example:
class Student {
String name;
int marks;
boolean isPassed() {
return marks >= 40;
}
void showResult() {
System.out.println(
name + " passed: " + isPassed()
);
}
}
The method:
showResult()
calls:
isPassed()
on the same current object.
94. Object Methods Can Call Other Objects#
Example:
class Printer {
void print(String text) {
System.out.println(text);
}
}
class Report {
void generate(Printer printer) {
printer.print("Generating report...");
}
}
The Report object collaborates with Printer.
95. Object Ownership#
When modeling a class, ask:
Which object should own this data?
For example:
BankAccount → balance
Student → marks
Product → price
Order → order status
The answer should make sense in the domain.
96. Object Responsibility#
Ask:
Which object should perform this operation?
Examples:
BankAccount → withdraw()
ShoppingCart → calculateTotal()
Student → calculateGrade()
Order → cancel()
This keeps behavior close to the data and rules it uses.
97. Data and Behavior Together#
Weak design:
Student data
+
many unrelated external functions
Object-oriented design can instead group:
Student data
+
Student behavior
Example:
class Student {
String name;
int marks;
char grade() {
if (marks >= 90) return 'A';
if (marks >= 75) return 'B';
if (marks >= 60) return 'C';
return 'D';
}
}
98. Why This Helps#
Now:
student.grade();
clearly communicates:
calculate the grade of this student
instead of:
calculateGrade(student);
Both can be valid designs, but object-oriented modeling often makes responsibilities explicit.
99. Class as an API#
A class exposes operations that other code can use.
Example:
class Counter {
void increment() {
...
}
int getValue() {
...
}
}
Other code can use:
counter.increment();
counter.getValue();
The class can hide implementation details later through access control.
100. Public and Private Preview#
Example:
class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
}
}
The field is private.
The method is public.
This is an early example of encapsulation.
101. Why Fields Should Not Always Be Public#
If everything is public:
account.balance = -100000;
external code can create invalid state.
Controlled methods can protect rules.
Chapter 15 will cover this deeply.
102. Object Construction Without Explicit Constructor#
Consider:
class Student {
String name;
}
Then:
Student s = new Student();
Java can provide an implicit default constructor when no constructor is explicitly declared.
Constructors themselves are covered in Chapter 13.
103. Why Constructors Are Separate#
A constructor is special syntax used during object creation.
Example:
Student s = new Student("Aman");
The constructor can initialize the object's fields.
You should understand classes and objects first, which is why constructors come next.
104. Object Lifecycle — Basic View#
A simplified lifecycle:
class available
↓
object created
↓
object initialized
↓
object used
↓
references change/disappear
↓
object may become unreachable
↓
eligible for garbage collection
Garbage collection is studied later in Chapter 44.
105. Reachability#
An object can remain reachable through references.
Example:
Student a = new Student();
Student b = a;
Even if:
a = null;
the object can still be reached through:
b
106. Object Becomes Unreachable#
Example:
Student a = new Student();
a = null;
If no other reference points to that object, it may become unreachable.
It can then become eligible for garbage collection.
107. Garbage Collection Is Not Manual Destruction#
Java does not provide a C++-style delete operation for ordinary object destruction.
You generally make objects unreachable and the garbage collector manages reclamation.
The exact timing of garbage collection is not guaranteed.
108. Object Equality Basics#
There are two common ideas:
identity
logical equality
Identity:
a == b
for references.
Logical equality:
a.equals(b)
if the class defines equality appropriately.
109. Example — Identity#
Student a = new Student();
Student b = new Student();
System.out.println(a == b);
Output:
false
They are different instances.
110. Example — Same Object#
Student a = new Student();
Student b = a;
System.out.println(a == b);
Output:
true
Same object.
111. equals() Preview#
If a class does not override equals(), the inherited behavior is not automatically a field-by-field comparison.
Later, you will learn how classes can define logical equality using:
equals()
hashCode()
This is especially important in collections.
112. Object Class Preview#
Every Java class ultimately has Object as part of its class hierarchy, directly or indirectly, except that interfaces have a different relationship.
Important methods associated with Object include:
toString()
equals()
hashCode()
getClass()
These will be studied later.
113. Why toString() Matters#
If you write:
System.out.println(student);
Java needs a string representation of the object.
Classes can override:
toString()
to provide a useful representation.
This will be covered later.
114. Class Members and Object Members#
A class can have:
instance members
static members
Example:
class Student {
String name; // instance
static int count; // static
void study() { // instance
}
static void showCount() { // static
}
}
Chapter 14 will explain the distinction deeply.
115. Instance Field Sharing#
Instance fields are not normally shared between objects.
Example:
class Student {
String name;
}
Then:
Student a = new Student();
Student b = new Student();
There are two name fields, one in each object.
116. Static Field Sharing Preview#
Static fields are associated with the class.
Example:
class Student {
static int count;
}
There is one class-level count rather than one independent count per object.
Detailed static behavior is Chapter 14.
117. Object Creation with Variables#
You can create:
Student a = new Student();
Student b = new Student();
Student c = new Student();
Each declaration creates a separate reference variable.
Each new Student() creates a separate object.
118. Anonymous Object#
You can create an object without storing its reference:
new Student();
This creates a Student object but gives you no reference through which to use it afterward.
This can be useful in limited cases, but normally you store the reference if you need the object.
119. Example of Anonymous Object#
class Printer {
void print() {
System.out.println("Hello");
}
}
public class Main {
public static void main(String[] args) {
new Printer().print();
}
}
Output:
Hello
A Printer object is created and immediately used.
120. Chained Method Calls#
If a method returns an object/reference, calls can sometimes be chained.
Example:
builder.append("Hello")
.append(" World");
This style is common with builders and fluent APIs.
You will see this more often with StringBuilder and other APIs.
121. Object Reference Assignment#
Consider:
Student a = new Student();
Student b = a;
This does not copy the object.
It copies the reference value.
Therefore:
one object
two references
122. Object Copy Is Not Automatic#
This:
Student b = a;
does not create a new Student object.
If you want a separate object, you need to create one and copy the desired state.
For example:
Student b = new Student();
b.name = a.name;
This is only a shallow/manual field copy and has limitations when fields themselves refer to mutable objects.
123. Shallow vs Deep Copy Preview#
Suppose:
class Student {
Address address;
}
If you copy:
b.address = a.address;
both Student objects refer to the same Address object.
That is shared nested state.
A deep copy would create a separate Address object too.
Copying strategies will be explored as OOP and object design become more advanced.
124. Object as a Method Result#
Example:
class Calculator {
int square(int x) {
return x * x;
}
}
The method returns a primitive.
But methods can also return objects:
Student createStudent() {
return new Student();
}
The return type determines what kind of value is returned.
125. Method Returning Current Object#
An instance method can return:
this
Example:
class Builder {
Builder step() {
return this;
}
}
This pattern supports fluent APIs.
this will be studied in Chapter 14.
126. Class Can Have Other Class Fields#
Example:
class Engine {
int power;
}
class Car {
String brand;
Engine engine;
}
This is a composition relationship.
A Car contains a reference to an Engine.
127. Passing Multiple Objects#
Methods can accept several object references:
static void compare(Student a, Student b) {
System.out.println(a.name);
System.out.println(b.name);
}
Usage:
compare(student1, student2);
128. Returning One of Multiple Objects#
Example:
static Student older(Student a, Student b) {
if (a.age >= b.age) {
return a;
}
return b;
}
The method returns a reference to one of the existing objects.
It does not automatically create a new object.
129. Object References and Conditional Logic#
Example:
if (student != null) {
student.study();
}
This checks whether the reference identifies an object before using it.
130. Object References and Loops#
Example:
for (Student student : students) {
if (student != null) {
System.out.println(student.name);
}
}
This is useful when an object array may contain null elements.
131. Object Arrays with for Loop#
Student[] students = new Student[3];
students[0] = new Student();
students[1] = new Student();
students[2] = new Student();
students[0].name = "Aman";
students[1].name = "Riya";
students[2].name = "Raj";
for (int i = 0; i < students.length; i++) {
System.out.println(students[i].name);
}
Output:
Aman
Riya
Raj
132. Enhanced for Loop with Objects#
for (Student student : students) {
System.out.println(student.name);
}
This is often cleaner when you do not need the index.
133. Object Array and null#
Example:
Student[] students = new Student[3];
students[0] = new Student();
students[2] = new Student();
Now:
students[0] → Student
students[1] → null
students[2] → Student
So this is unsafe:
for (Student student : students) {
System.out.println(student.name);
}
because the middle element is null.
134. Safe Object Array Loop#
for (Student student : students) {
if (student != null) {
System.out.println(student.name);
}
}
135. Class Fields Can Be Arrays#
A class can contain an array.
Example:
class Student {
String name;
int[] marks;
}
Usage:
Student student = new Student();
student.name = "Aman";
student.marks = new int[] {90, 85, 95};
136. Class Fields Can Be Collections#
Later you may see:
class Student {
List<String> subjects;
}
This is another example of an object containing references to other objects.
Collections will be covered in Chapters 31–35.
137. Object Model Example — Student#
class Student {
String name;
int age;
int[] marks;
double averageMarks() {
int total = 0;
for (int mark : marks) {
total += mark;
}
return (double) total / marks.length;
}
}
Usage:
Student student = new Student();
student.name = "Aman";
student.age = 20;
student.marks = new int[] {80, 90, 100};
System.out.println(student.averageMarks());
Output:
90.0
138. Why Object State Matters#
The result of an instance method can depend on the object's current state.
Example:
student.averageMarks();
depends on:
student.marks
Changing the marks changes the result.
139. Object Behavior Should Use Its Own State#
Example:
class BankAccount {
double balance;
boolean canWithdraw(double amount) {
return amount <= balance;
}
}
Calling:
account.canWithdraw(500);
uses the state of that account.
140. Objects Can Have Invariants#
An invariant is a rule that should remain true for valid objects.
Examples:
age >= 0
marks between 0 and 100
balance not negative
price >= 0
Good class design protects these rules.
Encapsulation and constructors will help with this.
141. Why Direct Field Access Can Be Dangerous#
Example:
class BankAccount {
double balance;
}
Anyone with access can write:
account.balance = -50000;
This may violate the intended rules.
Later:
private double balance;
plus controlled methods can improve the design.
142. Object Creation vs Initialization#
These concepts are related but not identical.
Object creation:
new Student()
Initialization includes setting the object's fields to appropriate initial values.
Constructors participate in initialization.
143. Object Initialization Order Preview#
When creating an object, Java performs initialization steps involving:
memory/object creation
field initialization
instance initialization blocks
constructor execution
The exact rules become important with constructors and inheritance.
Do not try to memorize all initialization rules yet.
144. Class Definition Does Not Create Instances#
Writing:
class Student {
String name;
}
does not create a Student object.
It defines the type.
Instances are created when code executes something such as:
new Student()
145. One Class, Many Objects#
This is one of the strongest benefits of classes.
Example:
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
All use the same class definition.
Each can maintain separate instance state.
146. Objects Can Be Created Dynamically#
Objects are normally created during program execution.
Example:
int count = 3;
Student[] students = new Student[count];
for (int i = 0; i < count; i++) {
students[i] = new Student();
}
The number of objects can depend on runtime data.
147. Class Reference Compatibility#
A reference variable's declared type controls what members are available at compile time.
Example:
Student student = new Student();
The declared type is:
Student
This topic becomes more important with inheritance and polymorphism.
148. Declared Type vs Actual Object#
For now:
Student student = new Student();
both are Student.
Later you will see:
Animal animal = new Dog();
where:
declared type → Animal
actual object → Dog
This is a foundation for runtime polymorphism.
149. Class Members and Access#
Suppose:
class Student {
private String name;
public void showName() {
System.out.println(name);
}
}
External code cannot directly access:
student.name
but can call:
student.showName();
This is controlled access.
150. Public Class Basics#
A class can be declared:
public class Student {
}
A public top-level class has additional source-file naming rules.
For example, normally:
Student.java
contains:
public class Student
Java also permits multiple top-level classes in one source file subject to visibility and file naming rules.
The rule about one public top-level class per source file will be discussed again when packages and compilation are covered.
151. Nested Classes Preview#
A class can also be declared inside another class.
Example:
class Outer {
class Inner {
}
}
Java supports several forms of nested classes.
Nested classes are covered later in the course.
152. Records Preview#
Modern Java also provides records for concise data-oriented classes.
Example:
record Point(int x, int y) {
}
This is not a replacement for every normal class.
Records will be discussed in the modern Java features chapter.
153. Practical Program — Student#
class Student {
String name;
int age;
int marks;
void showDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
boolean isPassed() {
return marks >= 40;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "Aman";
student.age = 20;
student.marks = 85;
student.showDetails();
System.out.println(
"Passed: " + student.isPassed()
);
}
}
Output:
Name: Aman
Age: 20
Marks: 85
Passed: true
154. Practical Program — Multiple Students#
class Student {
String name;
int marks;
void showResult() {
System.out.println(
name + " scored " + marks
);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.name = "Aman";
s1.marks = 90;
s2.name = "Riya";
s2.marks = 95;
s3.name = "Raj";
s3.marks = 82;
s1.showResult();
s2.showResult();
s3.showResult();
}
}
Output:
Aman scored 90
Riya scored 95
Raj scored 82
155. Practical Program — Rectangle#
class Rectangle {
double length;
double width;
double area() {
return length * width;
}
double perimeter() {
return 2 * (length + width);
}
boolean isSquare() {
return length == width;
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.length = 10;
rectangle.width = 5;
System.out.println(
"Area = " + rectangle.area()
);
System.out.println(
"Perimeter = " + rectangle.perimeter()
);
System.out.println(
"Square = " + rectangle.isSquare()
);
}
}
Output:
Area = 50.0
Perimeter = 30.0
Square = false
156. Practical Program — Bank Account#
class BankAccount {
String owner;
double balance;
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
boolean withdraw(double amount) {
if (amount <= 0 || amount > balance) {
return false;
}
balance -= amount;
return true;
}
void showBalance() {
System.out.println(
owner + ": " + balance
);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.owner = "Aman";
account.balance = 1000;
account.deposit(500);
account.withdraw(300);
account.showBalance();
}
}
Output:
Aman: 1200.0
This is still a simple version. Encapsulation will make the design stronger.
157. Practical Program — Car#
class Car {
String brand;
int speed;
void accelerate() {
speed += 10;
}
void brake() {
if (speed >= 10) {
speed -= 10;
}
}
void showSpeed() {
System.out.println(
brand + " speed = " + speed
);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.brand = "Toyota";
car.accelerate();
car.accelerate();
car.brake();
car.showSpeed();
}
}
Output:
Toyota speed = 10
158. Practical Program — Employee#
class Employee {
String name;
double monthlySalary;
double annualSalary() {
return monthlySalary * 12;
}
void showDetails() {
System.out.println("Name: " + name);
System.out.println(
"Monthly salary: " + monthlySalary
);
System.out.println(
"Annual salary: " + annualSalary()
);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
employee.name = "Riya";
employee.monthlySalary = 60000;
employee.showDetails();
}
}
Output:
Name: Riya
Monthly salary: 60000.0
Annual salary: 720000.0
159. Practical Program — Object as Argument#
class Student {
String name;
}
class Printer {
void printStudent(Student student) {
System.out.println(
"Student: " + student.name
);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "Aman";
Printer printer = new Printer();
printer.printStudent(student);
}
}
Output:
Student: Aman
160. Practical Program — Return an Object#
class Student {
String name;
int marks;
}
class StudentFactory {
static Student create(String name, int marks) {
Student student = new Student();
student.name = name;
student.marks = marks;
return student;
}
}
public class Main {
public static void main(String[] args) {
Student student =
StudentFactory.create("Aman", 90);
System.out.println(student.name);
System.out.println(student.marks);
}
}
Output:
Aman
90
161. Practical Program — Object Array#
class Student {
String name;
int marks;
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student();
students[1] = new Student();
students[2] = new Student();
students[0].name = "Aman";
students[0].marks = 90;
students[1].name = "Riya";
students[1].marks = 95;
students[2].name = "Raj";
students[2].marks = 82;
for (Student student : students) {
System.out.println(
student.name + " = " + student.marks
);
}
}
}
Output:
Aman = 90
Riya = 95
Raj = 82
162. Practical Program — Student with Address#
class Address {
String city;
String country;
}
class Student {
String name;
Address address;
void showAddress() {
System.out.println(
name + " lives in " +
address.city + ", " +
address.country
);
}
}
public class Main {
public static void main(String[] args) {
Address address = new Address();
address.city = "Mumbai";
address.country = "India";
Student student = new Student();
student.name = "Aman";
student.address = address;
student.showAddress();
}
}
Output:
Aman lives in Mumbai, India
163. Practical Program — Shared Nested Object#
class Address {
String city;
}
class Student {
String name;
Address address;
}
public class Main {
public static void main(String[] args) {
Address address = new Address();
address.city = "Mumbai";
Student a = new Student();
Student b = new Student();
a.name = "Aman";
b.name = "Riya";
a.address = address;
b.address = address;
address.city = "Pune";
System.out.println(a.address.city);
System.out.println(b.address.city);
}
}
Output:
Pune
Pune
Both Student objects refer to the same Address object.
164. Practical Program — Separate Nested Objects#
class Address {
String city;
}
class Student {
String name;
Address address;
}
public class Main {
public static void main(String[] args) {
Student a = new Student();
Student b = new Student();
a.name = "Aman";
b.name = "Riya";
a.address = new Address();
b.address = new Address();
a.address.city = "Mumbai";
b.address.city = "Pune";
System.out.println(a.address.city);
System.out.println(b.address.city);
}
}
Output:
Mumbai
Pune
Each Student has a separate Address object.
165. Practical Program — Counter Objects#
class Counter {
int value;
void increment() {
value++;
}
void show() {
System.out.println(value);
}
}
public class Main {
public static void main(String[] args) {
Counter a = new Counter();
Counter b = new Counter();
a.increment();
a.increment();
b.increment();
a.show();
b.show();
}
}
Output:
2
1
166. Practical Program — Object Identity#
class Student {
}
public class Main {
public static void main(String[] args) {
Student a = new Student();
Student b = new Student();
Student c = a;
System.out.println(a == b);
System.out.println(a == c);
}
}
Output:
false
true
167. Practical Program — Object Replacement#
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student a = new Student();
Student b = a;
a.name = "Aman";
a = new Student();
a.name = "Riya";
System.out.println(a.name);
System.out.println(b.name);
}
}
Output:
Riya
Aman
168. Practical Program — Null Check#
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student student = null;
if (student != null) {
System.out.println(student.name);
} else {
System.out.println("No student object");
}
}
}
Output:
No student object
169. Practical Program — Average Marks#
class Student {
String name;
int[] marks;
double average() {
int total = 0;
for (int mark : marks) {
total += mark;
}
return (double) total / marks.length;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "Aman";
student.marks = new int[] {
80, 90, 100
};
System.out.println(
student.name + " average = " +
student.average()
);
}
}
Output:
Aman average = 90.0
170. Practical Program — Find Older Student#
class Student {
String name;
int age;
}
public class Main {
static Student older(Student a, Student b) {
if (a.age >= b.age) {
return a;
}
return b;
}
public static void main(String[] args) {
Student a = new Student();
Student b = new Student();
a.name = "Aman";
a.age = 20;
b.name = "Riya";
b.age = 22;
Student result = older(a, b);
System.out.println(result.name);
}
}
Output:
Riya
171. Practical Program — Object Collaboration#
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
Engine engine;
void start() {
engine.start();
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Engine engine = new Engine();
Car car = new Car();
car.engine = engine;
car.start();
}
}
Output:
Engine started
Car started
172. Practice Exercise — Book#
Create:
class Book
Fields:
title
author
price
Methods:
showDetails()
isExpensive()
Create three objects.
173. Practice Exercise — Mobile#
Create:
class MobilePhone
Fields:
brand
model
price
battery
Methods:
call()
charge()
showDetails()
Create two different objects.
174. Practice Exercise — Employee#
Create:
class Employee
Fields:
name
id
salary
department
Methods:
work()
annualSalary()
showDetails()
Create at least three objects.
175. Practice Exercise — Product#
Create:
class Product
Fields:
name
price
quantity
Method:
totalPrice()
Example:
price = 100
quantity = 5
total = 500
176. Practice Exercise — Circle#
Create:
class Circle
Field:
radius
Methods:
area()
circumference()
diameter()
Use:
Math.PI
177. Practice Exercise — Bank Account#
Create:
BankAccount
with:
owner
balance
Methods:
deposit()
withdraw()
showBalance()
Try to prevent invalid withdrawals.
Later, convert this into a properly encapsulated class.
178. Practice Exercise — Rectangle#
Create:
Rectangle
Fields:
length
width
Methods:
area()
perimeter()
isSquare()
Create multiple Rectangle objects.
179. Practice Exercise — Student Array#
Create:
Student[] students = new Student[5];
Create five Student objects.
Store:
name
marks
Print all students.
Then find:
highest marks
lowest marks
average marks
180. Practice Exercise — Address#
Create:
Address
with:
city
state
country
Then create:
Student
with:
name
address
Print complete student information.
181. Practice Exercise — Object References#
Write a program:
Student a = new Student();
Student b = a;
Change state using a.
Read state using b.
Then create:
a = new Student();
and observe what happens.
Draw the reference diagram.
182. Practice Exercise — Object Return#
Write:
static Student createStudent(...)
that creates and returns a Student object.
Create three students using this method.
183. Practice Exercise — Object Comparison#
Create two Student objects with:
same name
same age
same marks
Check:
a == b
Explain why the result is what it is.
Then later study how equals() can represent logical equality.
184. Practice Exercise — Shared Address#
Create:
Address address
Create:
Student a
Student b
Assign the same Address object to both.
Change:
address.city
Observe both Students.
Then create separate Address objects and compare the behavior.
185. Interview Questions#
Q1. What is a class?#
A class is a Java type definition that describes members and behavior and can be used to create instances.
Q2. What is an object?#
An object is an instance of a class or another reference type.
Q3. What is the difference between a class and an object?#
Class → type/definition
Object → instance
Q4. Does declaring a reference create an object?#
No.
Student s;
only declares a reference variable.
Q5. What creates the object?#
An expression such as:
new Student()
creates a new Student instance.
Q6. What does new return?#
The new expression produces a reference to the newly created object.
Q7. What is a reference variable?#
A variable whose value can refer to an object.
Q8. Can multiple references point to the same object?#
Yes.
Student a = new Student();
Student b = a;
Q9. Does b = a copy the object?#
No.
It copies the reference value.
Q10. What is object state?#
The current values of the object's relevant instance fields.
Q11. What is object behavior?#
Operations that an object can perform, generally represented by methods.
Q12. What is an instance field?#
A non-static field associated with each object instance.
Q13. Do different objects have separate instance fields?#
Yes.
Each object has its own instance state.
Q14. What are default values of instance fields?#
They receive type-specific default values during object initialization if not otherwise initialized.
Examples:
int → 0
double → 0.0
boolean → false
reference → null
Q15. Do local variables receive the same automatic defaults?#
No.
Local variables must be definitely assigned before they are read.
Q16. What is null?#
A special reference value that means the reference does not currently identify an object.
Q17. What happens when you access a field using a null reference?#
A NullPointerException occurs.
Q18. What does == mean for object references?#
It checks whether the two references identify the same object.
Q19. What is the difference between == and equals()?#
For ordinary object references:
==
checks identity.
equals()
can represent logical equality when appropriately implemented.
Q20. Can two different objects have the same data?#
Yes.
Q21. Can two references point to one object?#
Yes.
Q22. Can one reference point to different objects at different times?#
Yes.
Q23. What is an object array?#
An array whose elements are references to objects.
Example:
Student[] students;
Q24. Does new Student[5] create five Student objects?#
No.
It creates an array containing five reference slots, initially null.
Q25. How do you create the actual Student objects?#
For example:
for (int i = 0; i < students.length; i++) {
students[i] = new Student();
}
Q26. Can a class contain another class's object?#
Yes.
For example:
class Car {
Engine engine;
}
Q27. What is object composition?#
It is building objects using references to other objects, often representing HAS-A relationships.
Q28. Are objects always stored on the heap?#
The Java language does not specify such a simplistic universal memory rule. JVM implementations may use different strategies.
For basic learning, focus on references and object identity rather than assuming a fixed physical location.
Q29. What is object lifecycle?#
At a high level:
creation
→ initialization
→ use
→ loss of reachability
→ possible garbage collection
Q30. What happens when an object becomes unreachable?#
It can become eligible for garbage collection, but Java does not guarantee exactly when collection occurs.
186. Output Questions#
Question 1#
class Student {
String name;
}
Student s = new Student();
System.out.println(s.name);
Output:
null
Question 2#
class Student {
int age;
}
Student s = new Student();
System.out.println(s.age);
Output:
0
Question 3#
class Student {
String name;
}
Student a = new Student();
Student b = new Student();
a.name = "Aman";
b.name = "Riya";
System.out.println(a.name);
System.out.println(b.name);
Output:
Aman
Riya
Question 4#
class Student {
String name;
}
Student a = new Student();
Student b = a;
a.name = "Aman";
System.out.println(b.name);
Output:
Aman
Question 5#
class Student {
}
Student a = new Student();
Student b = new Student();
System.out.println(a == b);
Output:
false
Question 6#
class Student {
}
Student a = new Student();
Student b = a;
System.out.println(a == b);
Output:
true
Question 7#
class Counter {
int value;
void increment() {
value++;
}
}
Counter a = new Counter();
Counter b = new Counter();
a.increment();
a.increment();
b.increment();
System.out.println(a.value);
System.out.println(b.value);
Output:
2
1
Question 8#
class Student {
String name;
}
static void change(Student s) {
s.name = "Changed";
}
Then:
Student s = new Student();
s.name = "Original";
change(s);
System.out.println(s.name);
Output:
Changed
Question 9#
class Student {
String name;
}
static void replace(Student s) {
s = new Student();
s.name = "New";
}
Then:
Student s = new Student();
s.name = "Original";
replace(s);
System.out.println(s.name);
Output:
Original
Question 10#
class Address {
String city;
}
class Student {
Address address;
}
Address address = new Address();
address.city = "Mumbai";
Student a = new Student();
Student b = new Student();
a.address = address;
b.address = address;
address.city = "Pune";
System.out.println(a.address.city);
System.out.println(b.address.city);
Output:
Pune
Pune
Question 11#
Student[] students = new Student[2];
System.out.println(students[0]);
Output:
null
The array contains reference slots, initially null.
Question 12#
Student[] students = new Student[2];
students[0] = new Student();
System.out.println(students[0]);
System.out.println(students[1]);
The first prints a Student object's default string representation unless toString() is overridden.
The second prints:
null
187. Common Mistakes#
Mistake 1 — Thinking declaration creates an object#
Wrong:
Student s;
means an object exists.
Correct:
It only declares a reference variable.
Mistake 2 — Thinking assignment copies the object#
Wrong:
Student b = a;
creates a new Student.
Correct:
It copies the reference value.
Mistake 3 — Forgetting new#
Wrong:
Student s;
s.name = "Aman";
This is invalid because s has not been assigned an object reference.
Mistake 4 — Calling a method on null#
Student s = null;
s.study();
Result:
NullPointerException
Mistake 5 — Assuming object arrays create objects#
Student[] students = new Student[5];
does not create five Student instances.
Mistake 6 — Using == for logical object equality#
student1 == student2
checks identity.
It does not automatically compare all fields.
Mistake 7 — Making all fields public#
This can allow invalid state.
Later you will learn encapsulation and controlled APIs.
Mistake 8 — Putting unrelated responsibilities into one class#
A class should ideally have a focused responsibility.
Mistake 9 — Confusing instance and static state#
Instance fields belong to each object.
Static fields are class-level.
Chapter 14 covers this deeply.
188. Class Design Checklist#
When creating a class, ask:
1. What concept does this class represent?
2. What data belongs to it?
3. What behavior belongs to it?
4. Which state should be protected?
5. Which operations should be public?
6. What other objects does it need?
7. Should fields be instance or static?
8. What should the object's initial state be?
9. What rules must always remain true?
10. Is the class doing unrelated jobs?
189. Object Modeling Example#
Problem:
Create a simple food delivery system.
Possible classes:
Customer
Restaurant
FoodItem
Order
DeliveryPartner
Address
Customer:
name
phone
address
Restaurant:
name
location
FoodItem:
name
price
Order:
items
customer
status
total
DeliveryPartner:
name
vehicle
This is object-oriented modeling.
190. Modeling Relationships#
Possible relationships:
Customer HAS-A Address
Order HAS-A Customer
Order HAS-A FoodItems
DeliveryPartner HAS-A Vehicle
These relationships will be studied formally in Chapter 22.
191. Mini Project — Student Management#
Build:
Student
with:
name
rollNumber
marks
Methods:
showDetails()
averageMarks()
isPassed()
Create several Student objects.
Then store them in:
Student[]
Add:
highest marks
lowest marks
class average
Later improve the project with constructors and encapsulation.
192. Mini Project — Library#
Create:
Book
Member
Library
Book:
title
author
available
Member:
name
memberId
Library:
books
members
Methods:
addBook()
registerMember()
borrowBook()
returnBook()
193. Mini Project — Bank#
Create:
BankAccount
Support:
deposit
withdraw
showBalance
Create multiple account objects.
Then add:
accountNumber
owner
transaction tracking
transfer
Later use encapsulation to protect balance.
194. Mini Project — Shopping Cart#
Create:
Product
Cart
Product:
name
price
Cart:
products
addProduct()
removeProduct()
calculateTotal()
Start with arrays if needed.
Later replace them with collections.
195. Mini Project — Employee Management#
Create:
Employee
Fields:
name
id
department
salary
Methods:
showDetails()
annualSalary()
Create an array of employees.
Find:
highest salary
lowest salary
average salary
196. Mini Project — Simple Game#
Create:
Player
Enemy
Player:
name
health
score
Enemy:
name
health
damage
Methods:
attack()
takeDamage()
Later use inheritance and polymorphism to make this more powerful.
197. Important Mental Model#
Always visualize:
REFERENCE
│
▼
OBJECT
│
┌──┴───────────────┐
↓ ↓
FIELDS METHODS
↓ ↓
STATE BEHAVIOR
For example:
student
│
▼
Student object
│
├── name
├── age
├── marks
│
├── study()
└── showResult()
198. The Most Important Difference#
Remember:
Student a = new Student();
Student b = a;
means:
one object
two references
NOT:
two objects
To create two objects:
Student a = new Student();
Student b = new Student();
199. Object Reference Mental Model#
Think of:
Student a = new Student();
as:
a ───────► Student object
Then:
Student b = a;
becomes:
a ───────┐
├────► Student object
b ───────┘
Then:
a = new Student();
becomes:
a ───────► Student object B
b ───────► Student object A
This diagram solves many beginner object questions.
200. Final Summary#
In this chapter you learned:
- What a class is
- Class as a type
- Class as a blueprint
- Object
- Instance
- new keyword
- Reference variables
- Object references
- Fields
- Instance fields
- Methods
- Instance methods
- Object state
- Object behavior
- Multiple objects
- Separate object state
- Object identity
- == with references
- null references
- NullPointerException
- Default field values
- Field initializers
- Local vs instance variables
- this preview
- Object parameters
- Pass-by-value for object references
- Returning objects
- Object composition
- Objects inside objects
- Arrays of objects
- null elements in object arrays
- Object graphs
- Mutable objects
- Immutable object preview
- Instance vs static preview
- Object lifecycle
- Reachability
- Garbage collection preview
- Object equality preview
- Object class preview
- Public/private preview
- Object design
- Object responsibilities
- Practical programs
- Exercises
- Mini projects
- Interview questions
- Output questions
201. Key Takeaways#
If you remember only the most important ideas, remember these:
1. A class defines a type.
2. An object is an instance.
3. new creates an object.
4. A reference variable can refer to an object.
5. Declaring a reference does not create an object.
6. Instance fields represent per-object state.
7. Instance methods can operate on the current object.
8. One class can create many objects.
9. Separate objects have separate instance state.
10. Multiple references can point to one object.
11. Assigning one object reference to another does not copy the object.
12. == checks reference identity for object references.
13. null means a reference identifies no object.
14. Dereferencing null causes NullPointerException.
15. Object arrays initially contain null references.
16. Objects can contain references to other objects.
17. Java passes object references by value.
18. A method can receive and return object references.
19. Good classes group related state and behavior.
20. Good object design assigns responsibilities sensibly.
202. What Comes Next?#
You now understand:
Class
↓
Object
↓
Reference
↓
State
↓
Behavior
The next major question is:
How do we create objects with the correct initial state?
For example, instead of:
Student student = new Student();
student.name = "Aman";
student.age = 20;
student.marks = 90;
we want:
Student student =
new Student("Aman", 20, 90);
This is where constructors become extremely important.
Chapter 13 — Constructors#
In the next chapter you will learn:
- What is a constructor?
- Why constructors are needed
- Default constructor
- No-argument constructor
- Parameterized constructor
- Constructor syntax
- Constructor invocation
- Constructor overloading
- Constructor chaining
- this()
- Constructor vs method
- Constructor access modifiers
- Constructor initialization
- Object initialization flow
- Constructor with validation
- Constructor with object fields
- Copy-style constructors
- Constructor best practices
- Common constructor mistakes
- Practical OOP programs
- Exercises
- Interview questions
Constructors are one of the most important concepts before moving into deeper OOP.