Sitemap

Chapter 3 — Java Variables

Mastering Java — One concept at a time

--

Variables are one of the simplest yet most essential concepts in Java.
Before we dive into methods, loops or OOPS, it’s important to understand how Java stores and manages data during execution.

In this chapter, we will learn:

  • What variables are
  • Different types of variables
  • How memory allocation works
  • Rules for naming variables
  • Defaults, constants and best practices

Let’s get started.

A variable in Java is a named memory location that stores a value while your program runs.

Think of it as a labelled container:

  • Label → variable name
  • The type of container → data type
  • What’s inside → value
int age = 25;

Here:

  • int → data type
  • age → variable name
  • 25 → value stored in memory

Types of Variables in Java

Java categorizes variables into three main types, based on where they are declared and how long they live.

Local Variables

Local variables are declared inside a method, constructor or block.
They exist only until the method finishes execution.

Characteristics

  • Must be initialized before use
  • Stored in the stack memory
  • Cannot have access modifiers (public, private, etc.)
void show() {
int count = 10; // local variable
System.out.println(count);
}

If you try to use a local variable without assigning a value, Java gives an error — because they do not receive default values.

Instance Variables

Instance variables (object variables) are declared inside a class but outside methods.

Each object has its own copy of these variables.

Characteristics

  • Represent the state of an object
  • Stored in the heap memory
  • Get default values if not initialized
class Car {
String model; // instance variable
int speed; // instance variable
}

Every new Car() object will have different model/speed values.

Static Variables (Class Variables)

Static variables belong to the class, not to objects.

They are declared with the static keyword.

Characteristics

  • Shared across all objects
  • Initialized only once (when class loads)
  • Useful for constants or shared data
class Employee {
static String company = "Google"; // static variable
}

All Employee objects share the same company name.

Default values in Java

Java automatically assigns default values to instance and static variables.

Local variables do not get defaults.

Press enter or click to view image in full size

Variable declaration rules (Java naming rules)

Java enforces strict naming conventions:

✔ Must start with a letter, _, or $
✔ Cannot start with a number
✔ Case-sensitive
✔ Cannot use Java keywords
✔ Should follow camelCase for readability

Get Srinath Venkatramanan’s stories in your inbox

Join Medium for free to get updates from this writer.

Valid:

int age;
double _salary;
String $name;

Invalid:

int 1value;       // starts with number
double class; // keyword

Final Variables — Making Constants

If you want a value that never changes, use final.

final double PI = 3.14159;

Once assigned, it cannot be modified.
Often written in UPPERCASE.

Memory View: How variables live in Java

The below separation helps Java manage memory efficiently and safely.

1️⃣Local Variables → Stored in the Stack

What is the Stack?

  • A small, fast memory area
  • Used for method execution
  • Stores: method calls, local variables, temporary values

Why local variables go here?

  • They exist only while the method runs
  • They disappear immediately when the method ends
void test() {
int x = 10; // local variable → stack
}

When test() finishes, x is deleted instantly.

2️⃣Instance Variables → Stored in the Heap

What is the Heap?

  • Large memory area
  • Stores objects
  • Data stays in memory until:
  • no reference is pointing to it
  • garbage collector removes it

Why instance variables go here?

  • Instance variables are part of objects
  • Objects live in the heap
class Car {
int speed; // instance variable
String model; // instance variable
}
Car c1 = new Car();   // object → heap

c1 (the object) is stored in the heap.
Inside that object, speed and model also live in the heap.

Each new object gets its own heap memory.

3️⃣Static Variables → Stored in Method Area (a part of JVM memory)

What is the Method Area?

  • A special region of JVM memory
  • It stores: class-level data, static variables, method bytecode, class metadata

Why static variables go here?

  • Static variables belong to the class, not the object
  • They are created when the class loads
  • They stay until program ends
class Employee {
static String company = "Google"; // static variable
}

company is stored once in the method area, not inside each Employee object.

Every object shares the same static value.

Why Java Separates These?

This design gives important benefits:

Efficiency

  1. Stack is fast → suitable for short-lived local variables
  2. Heap is flexible → suitable for objects that live longer
  3. Method area stores shared data → avoids duplication

Safety

  1. Variables stored separately prevent accidental interference.
  2. For example, modifying a static variable affects all objects — but modifying an instance variable affects only one object.

Memory Management

  1. Java’s garbage collector cleans the heap automatically.
  2. Stack memory clears itself after methods end.
  3. Method area stays stable for class-level data.

In the next chapter, we’ll dive into Java Data Types — the foundation that decides what kind of values your variables can hold.

If you found this chapter helpful, please do follow the series.

🖋️ Mastering Java — One Concept at a Time
Thanks for reading. See you in the next chapter!

--

--