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.
It defines:
Variables (state)
Methods (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.
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
}
Each object gets its own copy.
π§ Memory Concept β Stack vs Heap
When we write:
School hari = new School();
πΉ Stack Memory
Stores:
Reference variable (hari)
πΉ Heap Memory
Stores:
Object
Instance variables
Conceptually:
`> STACK------------------HEAP
hari -----------> tamil_mark = 60
-----------------> english_mark = 70`
π₯ Object = State + Behaviour
An object contains:
β State β Variables
β Behaviour β Methods
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)