Today was a very important day in my Java learning journey.
I moved from basic data types to the heart of Object-Oriented Programming:
πΉ Class
πΉ Object
πΉ Instance Variables
πΉ Heap & Stack Memory
This is where Java starts feeling powerful πͺ
π Variable Naming Rules in Java
Before jumping into classes, I revised variable rules.
β
Variable names should start with a lowercase letter
int tamilMark = 60;
int englishMark = 70;
Breakdown:
int β Data type
tamilMark β Variable name
= β Assignment operator
60 β Value
; β End of statement
Java does not allow duplicate variable names in the same scope.
π What is a Class in Java?
1οΈβ£ Class is a Java Keyword
class is a reserved keyword in Java.
It is used to create a blueprint for objects.
We cannot use class as a variable name because it is reserved.
Example:
class Student {
}
2οΈβ£ Class is a Template / Blueprint
A class is like a design or blueprint.
Class is a Blueprint
A class acts as a template to create objects
It contains:
Variables β represent data (state)
Methods β define actions (behavior)
Real-time Example:
Think of a PPT template:
You can add slides
Delete slides
Edit slides
Similarly:
A class can contain variables and methods.
Objects are created based on the class blueprint.
π― Slightly Detailed Answer
π
βA class in Java acts as a structure or blueprint.
It defines what data an object will store (variables) and what actions it can perform (methods).
Using this blueprint, we can create multiple objects with similar characteristics.β
π― Real-Time Add-on
π
βFor example, if I create a Student class, it defines name and marks as variables, and actions like studying or displaying details as methods.
Then I can create multiple student objects based on that class.β
3οΈβ£ Class Name First Letter Should Be Capital
This is a Java naming convention (not a strict rule, but strongly recommended).
β
Correct:
class School {
}
β Not recommended:
class school {
}
π Class names usually follow PascalCase:
StudentDetails
BankAccount
EmployeeData
4οΈβ£ Class Name Can Contain $ and _
Java allows:
Letters (AβZ, aβz)
Numbers (0β9)
$
_
Examples:
class Student_Data {}
class Student$Info {}
class Student2 {}
β Not allowed:
class 2Student {} // Cannot start with number
class Student-Info {} // - not allowed
5οΈβ£ Class Name Can Have Numbers
Yes, but:
β Numbers are allowed
β Class name cannot start with a number
Example:
class Student2026 {} // β Valid
class 2026Student {} // β Invalid
6οΈβ£ Class is a Logical Entity
A class does not occupy memory when defined.
It is just a logical structure.
Memory is allocated only when we create an object using new.
7οΈβ£ Class Syntax
Basic syntax:
class ClassName {
// variables
// methods
}
Example:
class School {
int tamil_mark;
int english_mark;
}
π¦ What is an Object?
Object Syntax
ClassName refName = new ClassName();
Example:
School hari = new School();
Explanation:
School β Class name
hari β Reference variable
new β Java keyword (allocates memory in heap)
School() β Constructor
Object is a Combination of State and Behavior
State β Variables
int tamil_mark;
int english_mark;
Behavior β Methods
void study() {
System.out.println("Student is studying");
}
Object contains both state and behavior.
Object is a Physical Entity
When we create:
School hari = new School();
JVM allocates memory in Heap memory
The object becomes a physical entity in memory.
π₯ Simple Real Example (Your Marks Example)
class School {
int tamil_mark;
int english_mark;
}
public class Main {
public static void main(String[] args) {
School hari = new School();
hari.tamil_mark = 60;
hari.english_mark = 70;
School bala = new School();
bala.tamil_mark = 70;
bala.english_mark = 50;
System.out.println(hari.tamil_mark);
System.out.println(bala.tamil_mark);
}
}
π§± Instance Variables
Variables declared inside a class but outside methods are called instance variables.
class School {
int tamil_mark; // Instance variable
int english_mark; // Instance variable
}
π§ Stack vs Heap
When you write:
School hari = new School();
This line actually does 2 things:
πΉ 1. Stack Memory (Reference)
hari is a reference variable
It is stored in Stack
It does NOT store the actual object
It only stores the address (reference) of the object
π Think: βhari knows where the object isβ
Each object gets its own copy.
πΉ 2. Heap Memory (Actual Object)
new School() creates an object
This object is stored in Heap
It contains:
Instance variables
(tamil_mark = 60, english_mark = 70)
π Think: βActual data lives hereβ
πΉ How They Are Connected
STACK HEAP
----- ------------------
hari -------------> School Object
tamil_mark = 60
english_mark = 70
π hari β points to the object in heap
πΉ Real-Life Example π
Stack β Address written on paper
Heap β Actual house
π hari = address
π Object = house
You donβt live in the addressβ¦ you use it to find the house!
πΉ Important Points
Stack stores reference variables
Heap stores actual objects + data
One object can have multiple references
If no reference points to an object β Garbage Collector removes it
πΉ Simple One-Line Answer
π
βStack stores reference variables, while Heap stores actual objects and their data. The reference variable points to the object in heap.β
Example:
class School {
int tamil_mark;
void study() {
System.out.println("Student is studying");
}
}
Object combines both data and functionality.
π― Final Summary
Concept----------------------Meaning
Class------------------------Blueprint
Object-----------------------Real-world instance
new--------------------------Allocates memory in heap
Instance variable------------Belongs to object
Stack------------------------Stores reference
Heap-------------------------Stores object
π― Key Takeaways β Day 4
β Class is a blueprint
β Object is a physical entity
β Memory is allocated using new
β Heap stores objects
β Stack stores reference variables
β Instance variables belong to objects
Tomorrow I will explore:
π Types of variables
π Operators in Java
π More about memory
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)