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.

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 {
}
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

🧠 Stack vs Heap

When you write:

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

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

πŸ‘‰ 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");
    }
}
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

πŸ€– 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)