DEV Community

Cover image for 🚀 Day 4 of My Automation Journey – Understanding Class, Object & Memory in Java
bala d kaveri
bala d kaveri

Posted on • Edited on

🚀 Day 4 of My Automation Journey – Understanding Class, Object & Memory in Java

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 {
}
Enter fullscreen mode Exit fullscreen mode

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 {
}
Enter fullscreen mode Exit fullscreen mode

❌ Not recommended:

class school {
}
Enter fullscreen mode Exit fullscreen mode

👉 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 {}
Enter fullscreen mode Exit fullscreen mode

❌ Not allowed:

class 2Student {}   // Cannot start with number
class Student-Info {} // - not allowed
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Example:

class School {
    int tamil_mark;
    int english_mark;
}
Enter fullscreen mode Exit fullscreen mode

📦 What is an Object?
Object Syntax

ClassName refName = new ClassName();
Enter fullscreen mode Exit fullscreen mode

Example:

School hari = new School();
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

Object contains both state and behavior.
Object is a Physical Entity
When we create:

School hari = new School();
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

🧱 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
}
Enter fullscreen mode Exit fullscreen mode

Each object gets its own copy.

🧠 Memory Concept – Stack vs Heap

When we write:

School hari = new School();
🔹 Stack Memory
Enter fullscreen mode Exit fullscreen mode

Stores:

Reference variable (hari)

🔹 Heap Memory
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Date: 23/02/2026
Trainer: Nantha from Payilagam

🤖 A Small Note
I used ChatGPT to help me structure and refine this blog.

Top comments (0)