JavaVariables

Java Variables

What Are Variables in Java?

In Java, a variable is a named memory location used to store data during the execution of a program. Variables allow your program to hold values — such as numbers, text, or objects — and manipulate them dynamically.

Each variable in Java has:

  • A data type (e.g., int, String, boolean)

  • A name (identifier)

  • An optional initial value

Syntax of Variable Declaration

Java
dataType variableName = value;

Examples

Java
int age = 25;
String city = "Delhi";
double salary = 55000.75;

You can also declare multiple variables of the same type:

Java
int x = 10, y = 20, z = 30;
Types of Variables in Java

Java supports three main types of variables:

Type

Declared In

Scope

Lifetime

Local Variable

Inside methods/blocks

Within the method/block only

During method execution

Instance Variable

Inside class (no static)

Per object

As long as object exists

Static Variable

Inside class (with static)

Shared across all objects

As long as class is loaded

1. Local Variables
  • Declared inside methods, constructors, or blocks

  • Must be initialized before use

  • Not accessible outside their scope

  • No default values assigned

Example

Java
public class Calculator {

    public void addNumbers() {
        int a = 10; // Local variable
        int b = 20;
        int sum = a + b;
        System.out.println("Sum: " + sum);
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
        calc.addNumbers();
    }
}
2. Instance Variables
  • Declared inside a class but outside any method

  • Belong to each object of the class

  • Can have access modifiers (private, public, etc.)

  • Automatically initialized with default values

Example

Java
public class Student {

    // Instance variables
    String name;
    int rollNumber;

    public void setDetails(String n, int r) {
        name = n;
        rollNumber = r;
    }

    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Roll Number: " + rollNumber);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();

        s1.setDetails("Aman", 101);
        s2.setDetails("Neha", 102);

        s1.display();
        s2.display();
    }
}
3. Static Variables (Class Variables)
  • Declared using the static keyword

  • Shared among all instances of the class

  • Memory allocated only once at class loading time

  • Often used for constants or counters

Example

Java
public class BankAccount {

    // Static variable
    static String bankName = "National Bank";

    // Instance variables
    String accountHolder;
    double balance;

    public void setAccount(String holder, double amount) {
        accountHolder = holder;
        balance = amount;
    }

    public void showAccount() {
        System.out.println("Bank: " + bankName);
        System.out.println("Account Holder: " + accountHolder);
        System.out.println("Balance: " + balance);
    }

    public static void main(String[] args) {
        BankAccount acc1 = new BankAccount();
        BankAccount acc2 = new BankAccount();

        acc1.setAccount("Rahul", 15000);
        acc2.setAccount("Priya", 20000);

        acc1.showAccount();
        acc2.showAccount();
    }
}
Variable Naming Rules
  • Must begin with a letter, _, or $

  • Cannot begin with a digit

  • Cannot use Java reserved keywords

  • Should be meaningful and descriptive

Tip
Valid: age, totalAmount, _count, $price. Invalid: 1value, class, void
Default Values of Instance and Static Variables

Data Type

Default Value

int

0

float

0.0f

boolean

false

char

\u0000

String

null

Warning
Local variables do not get default values — you must initialize them manually.
Best Practices
Tip
Use meaningful variable names (totalMarks, studentName). Keep variable scope as narrow as possible. Use final for constants. Avoid global/static variables unless necessary. Use getters/setters for instance variables (encapsulation).
Recap Program — All Variable Types Together

Java
public class VariableTypesDemo {

    int instanceVar = 100;         // Instance variable
    static int staticVar = 200;    // Static variable

    public void showValues() {
        int localVar = 300;        // Local variable
        System.out.println("Instance Variable: " + instanceVar);
        System.out.println("Static Variable: " + staticVar);
        System.out.println("Local Variable: " + localVar);
    }

    public static void main(String[] args) {
        VariableTypesDemo obj = new VariableTypesDemo();
        obj.showValues();
    }
}
Instance Variable: 100
Static Variable: 200
Local Variable: 300