DEV Community

Cover image for πŸš€ Day 5 of My Automation Journey – Instance, Static Variables & Operators in Java
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 5 of My Automation Journey – Instance, Static Variables & Operators in Java

Today I learned about:

πŸ”Ή Instance Variables/ Static Variables
πŸ”Ή Difference between Instance and Static Variables
πŸ”Ή How memory behaves with static
πŸ”Ή Introduction to Operators in Java

This concept helped me understand how data can be shared across all objects in Java πŸ”₯

πŸ“Œ What is a Static Variable?

A static variable belongs to the class, not to the object.

πŸ‘‰ It is common for all objects.
πŸ‘‰ Only one copy is created in memory.
πŸ‘‰ It is shared across every object of that class.

🧠 Instance Variable vs Static Variable
Instance Variable---------- Static Variable
Belongs to object---------- Belongs to class
Separate copy for each object---Single copy shared by all
Created inside heap with object Created once in class memory
🏫 Example – School Marks Program

Let’s understand using your example.

package java_module_1;

public class School_Vikas {

    // Instance variables
    int tam_mark;
    int eng_mark;

    // Static variable
    static String School_Name = "Vikas School";

    public static void main(String[] args) {

        School_Vikas rajiv = new School_Vikas();
        rajiv.tam_mark = 60;
        rajiv.eng_mark = 50;

        System.out.println("Rajiv's Tamil mark is " + rajiv.tam_mark);
        System.out.println("Rajiv's English mark is " + rajiv.eng_mark);

        // Calling static variable
        System.out.println("School Name " + School_Vikas.School_Name);

        // We can also call directly inside same class
        System.out.println(School_Name);

        School_Vikas kumar = new School_Vikas();
        kumar.tam_mark = 55;
        kumar.eng_mark = 45;

        System.out.println("Kumar's Tamil mark is " + kumar.tam_mark);
        System.out.println("Kumar's English mark is " + kumar.eng_mark);
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” What Happens Here?
βœ… Instance Variables
int tam_mark;
int eng_mark;

Each object gets its own copy.

So:

rajiv has separate marks

kumar has separate marks

Memory in Heap:

rajiv β†’ tam_mark = 60
eng_mark = 50

kumar β†’ tam_mark = 55
eng_mark = 45
βœ… Static Variable
static String School_Name = "Vikas School";

Only ONE copy is created.

Both objects share the same school name.

Memory:

Class Area:
School_Name = "Vikas School"

Even if we create 100 students,
School_Name remains one common value.

πŸ“Œ How to Access Static Variable
βœ” Using Class Name (Recommended)

School_Vikas.School_Name
Enter fullscreen mode Exit fullscreen mode

βœ” Directly inside same class

System.out.println(School_Name);
Enter fullscreen mode Exit fullscreen mode

🧠 Important Rule

Static variable:

Is created when class is loaded

Does NOT require object creation

Is stored in class memory (Method Area)

πŸ”₯ Why Do We Use Static?

We use static when:

βœ” Value is common for all objects
βœ” Data should not change per object
βœ” Utility or shared configuration

Example:

School Name
College Code
Company Name

Interest Rate (bank example)

βž• Introduction to Operators in Java

An operator is a special symbol used to perform operations.

Example:

  • - * / % πŸ“š Types of Operators 1️⃣ Assignment Operator =

Example:

int a = 10;
2️⃣ Arithmetic Operators

  • - * / %

Example:

int total = 10 + 5;
3️⃣ Unary Operators
++ --

Example:

int a = 5;
a++; // Increment
a--; // Decrement
Types:

Post Increment β†’ a++

Pre Increment β†’ ++a

Post Decrement β†’ a--

Pre Decrement β†’ --a

4️⃣ Relational Operators
== != < > <= >=

Used for comparison.

5️⃣ Conditional Operators
&& ||

&& β†’ AND

|| β†’ OR

🎯 Key Takeaways – Day 5

βœ” Static variable is shared by all objects
βœ” Instance variable belongs to each object
βœ” Static can be accessed using class name
βœ” Only one copy of static exists in memory
βœ” Operators help perform calculations and comparisons

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

πŸ€– A Small Note
I used ChatGPT to help me structure and refine this blog.

Top comments (0)