1. Introduction
Java is an object-oriented programming (OOP) language, and the class is the fundamental building block of every Java program. A class is a blueprint or template that defines the structure (fields) and behavior (methods) that its objects will have. An object is a concrete instance of a class, created at runtime, with its own copy of state stored in memory.
Cricket analogy: A batting technique manual is like a class defining stance and grip, while Virat Kohli stepping onto the crease to bat is the object—a real instance applying that blueprint with his own bat and score.
Understanding classes and objects is essential because almost everything in Java — from simple data holders to complex frameworks — is built using this concept. A single class can be used to create many objects, each with independent field values but sharing the same defined behavior.
Cricket analogy: The 'fast bowler' class in a coaching manual is used to produce Jasprit Bumrah, Mitchell Starc, and Trent Boult—each bowler has different pace and accuracy stats but they all share the same bowling action methods.
2. Syntax
class ClassName {
// fields (instance variables)
dataType fieldName;
// methods
returnType methodName(parameters) {
// method body
}
}
// Creating an object
ClassName objectName = new ClassName();3. Explanation
A class does not occupy memory for its fields until an object is created; the class merely describes what fields and methods every object of that type will have. The new keyword allocates memory on the heap for the object and invokes a constructor to initialize it. Each object has its own copy of instance fields, but all objects share the same method code defined in the class.
Cricket analogy: A net-practice drill sheet describing cover-drive footwork takes no space on the field until a batsman like Rohit Sharma actually steps up—only then does the ground 'allocate' his stance, while the drill's technique stays the same for everyone.
Exam tip: A class is a logical construct (no memory allocated for instance fields at compile time); an object is a physical/runtime entity created using new, occupying heap memory.
Common trap: Students often confuse 'class variables' (static) with 'instance variables' (non-static). Only static fields are shared across all objects — instance fields are unique per object.
4. Example
public class Student {
// fields
String name;
int age;
// method
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
// creating two objects of Student
Student s1 = new Student();
s1.name = "Asha";
s1.age = 21;
Student s2 = new Student();
s2.name = "Ravi";
s2.age = 23;
s1.display();
s2.display();
}
}5. Output
Name: Asha, Age: 21
Name: Ravi, Age: 236. Key Takeaways
- A class is a blueprint; an object is a runtime instance of that class.
- Each object has its own copy of instance fields; methods are shared across objects.
- The
newkeyword allocates heap memory and triggers constructor execution. - Multiple objects can be created from a single class, each independently modifiable.
- No memory is reserved for instance fields until an object is instantiated.
Practice what you learned
1. What is a class in Java?
2. Which keyword is used to create an object in Java?
3. Where are instance fields of an object stored?
4. What happens when a class is compiled but no object is created?
5. In the example, why do s1.display() and s2.display() print different values?
Was this page helpful?
You May Also Like
Constructors in Java
Understand Java constructors — default, parameterized, overloaded, and chained — with syntax rules and worked examples.
this Keyword in Java
Master the `this` keyword in Java — resolving field/parameter name conflicts, constructor chaining, and passing the current object.
Encapsulation in Java
Learn how encapsulation in Java bundles data and behavior together using private fields, access modifiers, and getters/setters.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics