Variables in Java

⏱️ 6 min read • Beginner Level • Lesson 10

Lesson 10 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

Variables in Java are named memory locations used to store data. They allow programs to save values and change them during execution. Understanding variables is essential before learning data types, operators, and control statements.


  • Variables are named storage locations in memory used to hold values.

    Every variable has a data type that decides the kind of values it can store (int, float, String, etc.).

  • They can take different values at different times during execution.

    A variable must be initialized before using it, otherwise the compiler will throw an error.

Syntax for Variable Declaration

Syntax.java
Copy Download
DataType identifier = value;

Types of Variables in Java

  1. Local Variables

    A local variable is declared inside a method or block and its scope is limited to that block.

    They don’t get default values and must be initialized before use.

  2. Instance Variables (Non-Static Fields)

    A non-static variable declared inside a class but outside methods is called an instance variable. Each object gets its own copy of instance variables.

    They are automatically initialized with default values (e.g., 0 for int, null for objects).

  3. Class Variables (Static Fields)

    A static variable is declared with the static keyword inside a class. It belongs to the class rather than any object.

    Static variables cannot be local.

    Since only one copy exists per class, changing it affects all objects.

Example:
Test.java
Copy Try Download
public class Test {
  int x = 50;        // instance variable
  static int y = 100; // static variable

  void myMethod() {
    int z = 150;     // local variable
  }
}
  
Explanation:
  • x is an instance variable.
  • y is a static variable.
  • z is a local variable.

Here: x is tied to an object, y is tied to the class, and z exists only during method execution.

Local variables must be initialized before use, while instance and static variables get default values.


Default Values of Instance and Class Variables

In Java, instance and class (static) variables are automatically initialized with default values if not explicitly assigned. Local variables do not have default values and must be initialized before use.

Data Type Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000' (null character)
boolean false
Objects (e.g., String) null

This table applies to both instance and class (static) variables. Local variables must be explicitly initialized, or the compiler will throw an error.


Important Notes
  • Local variables do not receive default values.
  • Instance variables belong to objects.
  • Static variables belong to the class.
  • Variable names should be meaningful.
Summary:
  • Variables are named memory locations in Java.
  • They can store changing values during execution.
  • Three types: local, instance, static.
  • Initialization rules differ by type.

Interview Questions ⭐

A variable is a named memory location used to store data values.

Java has three types of variables: instance variables, static variables, and local variables.

Local variables must be initialized before use, while instance and static variables have default values if not initialized.

Next step: Learn Java Data Types

🚀 Continue to Data Types →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Variables | Language: Java

Question 1 of 5
Q1. Identify the type of variable 'count' in the following code:
public class Counter {
    static int count = 0;
    void increment() {
        count++;
    }
}
Q2. What will be the output of the following code?
public class Test {
    int x;             // instance variable
    static int y;      // static variable
    public static void main(String[] args) {
        Test obj = new Test();
        System.out.println(obj.x);
        System.out.println(y);
    }
}
Q3. What is the default value of a 'boolean' instance variable in Java?
Q4. Consider this code snippet. What will be the output?
public class Test {
    int num = 10;  // instance variable
    public static void main(String[] args) {
        Test t1 = new Test();
        Test t2 = new Test();
        t1.num = 20;
        System.out.println(t1.num + " " + t2.num);
    }
}
Q5. Which of the following is TRUE about local variables in Java?
public class Example {
    public static void main(String[] args) {
        int a;  // local variable
        System.out.println(a);
    }
}

🎉 Great job! Continue learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.