100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Variables in Java

Learn what variables are in Java, how to declare and initialize them, naming rules, and the difference between local, instance, and static variables.

Basics & Data TypesBeginner6 min readJul 7, 2026
Analogies

1. Introduction

A variable in Java is a named memory location that holds a value which can be manipulated during program execution. Every variable in Java has a data type that determines the size and layout of the variable's memory, the range of values that can be stored, and the set of operations that can be applied to it.

🏏

Cricket analogy: A variable is like a labeled locker in the dressing room holding a player's gear; its type (batting kit vs bowling kit) determines what fits inside and what operations (bowling vs batting) can be done with it.

Java is a statically and strongly typed language, which means every variable must be declared with a specific type before it is used, and that type cannot change at runtime. This helps the compiler catch type-related errors early, before the program even runs.

🏏

Cricket analogy: Java is like a strict team-sheet rule where a player's role (bowler or batsman) is declared before the match and can't switch mid-innings; the team management (compiler) catches a miscategorized player before the match even starts.

2. Syntax

java
dataType variableName;              // declaration
dataType variableName = value;      // declaration with initialization

int age;                            // declared, not initialized
int marks = 90;                     // declared and initialized
String name = "Alice";              // reference type variable

3. Explanation

Java supports three main kinds of variables. A local variable is declared inside a method, constructor, or block and exists only for the duration of that block; it must be initialized before use because it has no default value. An instance variable is declared inside a class but outside any method, and each object of the class gets its own copy; it is automatically initialized to a default value (0, null, false, etc.). A static variable is declared with the static keyword and belongs to the class itself rather than any individual object, so it is shared across all instances.

🏏

Cricket analogy: A local variable is like chalk marks a fielder makes for a single over that vanish after it; an instance variable is like each player's personal kit that's theirs alone; a static variable is like the team's single shared trophy cabinet, common to everyone on the squad.

Variable names in Java must begin with a letter, underscore (_), or dollar sign ($), followed by letters, digits, underscores, or dollar signs. They are case-sensitive and cannot be a reserved keyword such as class or int. By convention, Java uses camelCase for variable names, e.g. totalMarks.

🏏

Cricket analogy: Naming a scoring variable is like naming a player — it must start with a proper letter (not a number like "1Kohli"), is case-sensitive (Sachin vs sachin differ), can't be a reserved term like "out," and convention favors camelCase like totalRuns.

Exam trap: A local variable in Java does NOT get a default value. Using an uninitialized local variable causes a compile-time error ('variable might not have been initialized'), unlike instance and static variables, which are auto-initialized (0/0.0/false/null).

4. Example

java
public class VariableDemo {
    static int staticVar = 100;      // static variable
    int instanceVar = 10;            // instance variable

    void show() {
        int localVar = 5;             // local variable, must be initialized
        System.out.println("Static: " + staticVar);
        System.out.println("Instance: " + instanceVar);
        System.out.println("Local: " + localVar);
    }

    public static void main(String[] args) {
        VariableDemo obj = new VariableDemo();
        obj.show();
    }
}

5. Output

text
Static: 100
Instance: 10
Local: 5

6. Key Takeaways

  • A variable is a named memory location with a fixed data type.
  • Java has three variable kinds: local, instance, and static.
  • Local variables must be explicitly initialized before use; they have no default value.
  • Instance and static variables get automatic default values.
  • Static variables are shared across all objects of a class.
  • Variable names are case-sensitive and cannot be reserved keywords.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#VariablesInJava#Variables#Syntax#Explanation#Example#StudyNotes#SkillVeris