Variables in Java

Variables are named locations in memory that hold values that may be modified by the program. A variable can take different values at different times during execution.


  • 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

DataType identifier = value; // value is optional

Here, DataType specifies the type of value (e.g., int, float), identifier is the variable name, and value is an optional initialization.

Example:
int a;           // declaration only
int x = 100;     // declaration + initialization
float f = 2.3f;  // float requires 'f' at the end

If a variable is declared but not initialized, it may have a default value (for instance & static variables) or cause an error (for local variables).


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 Code
public class Test {
  int x = 50;        // instance variable
  static int y = 100; // static variable

  void myMethod() {
    int z = 150;     // 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.


Summary

  • Variables are named memory locations that hold values which can change during program execution.
  • They must be declared with a data type and can be optionally initialized.
  • Java has three main types of variables: local, instance (non-static), and class (static) variables, each with different scopes and lifetimes.
  • Local variables exist only within methods and must be initialized before use, while instance and static variables get default values.

Understanding variable types and their scopes is crucial for effective Java programming.


Next: Data Types in Java




๐Ÿš€ Quick Knowledge Check

Topic: Variables | Language: Java

Q1. What is the default value of a 'boolean' instance variable in Java?
Q2. 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);
    }
}
Q3. Identify the type of variable 'count' in the following code:
public class Counter {
    static int count = 0;
    void increment() {
        count++;
    }
}
Q4. 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);
    }
}
Q5. 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);
    }
}