Java Variable

In Java, a variable is a container that stores data. Variables allow you to hold different types of data, such as numbers, text, or more complex objects, which can be used and manipulated in a program.

A variable is a identifier that denotes a storage location used to store data value.

You should think of a variable as a container or box where you can store data.

Note: A variable may take a different values at a different times during the execution of the program.

Syntax: To declare a variable in Java:


dataType variableName = value;

Explanation:

dataType: Specifies the type of data the variable will hold (e.g., int, double, String).

variableName: The name of the variable (must follow Java naming rules).

value: The data assigned to the variable (optional during declaration).

Example:


int age = 40; // Declares an integer variable named 'age' with a value of 40.

Rules for Naming Variables

1. Must begin with a letter, _, or $.

2. Cannot use Java keywords (e.g., int, class, etc.).

3. Case-sensitive (MyVar and myvar are different).

4. Should follow camelCase for readability (e.g., studentAge).

5. White Space is not allowed.

6. Variable names can be of any length.

Scope of Variables

Java variables can be categorized into three main types:

1. Local Variables

Variable declared and used inside the method called local variables.

Note:

  1. Declared inside a method, constructor, or block.
  2. Only accessible within the scope where they are defined.
  3. No default value; must be initialized before use.

Example:


public void displayAge() {
    int age = 40; // Local variable
    System.out.println("Age: " + age);
}

2. Instance Variables

Instance variable is declared inside the class.

Note:

1. Declared inside a class but outside any method, constructor, or block.

2. Each object of the class has its own copy.

3. Default values.

int -> 0
double -> 0.0
boolean -> false
String (or any object) -> null

Example:


public class Person {
    String name; // Instance variable
    int age;     // Instance variable

    public void displayDetails() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

3. Static Variables (Class Variables)

Declared with the static keyword.

Shared among all instances of the class.

Memory is allocated once when the class is loaded.

Example:


public class Counter {
    static int count = 0; // Static variable

    public Counter() {
        count++;
    }

    public static void displayCount() {
        System.out.println("Count: " + count);
    }
}

Java Variable – Questions and Answers

Q 1: What is a variable in Java?

Ans: A variable stores data values used by a program.

Q 2: How many types of variables are there in Java?

Ans: Local, instance, and static variables.

Q 3: What is a local variable?

Ans: Declared inside a method and accessible only within it.

Q 4: What is an instance variable?

Ans: Belongs to an object of a class.

Q 5: What is a static variable?

Ans: Shared among all instances of a class.

Java Variable – Objective Questions (MCQs)

Q1. Which of the following is the correct way to declare an integer variable in Java?






Q2. What is the default value of a boolean variable in Java?






Q3. Which of the following variable types are valid in Java?






Q4. What will be the output of the following Java code?


int x = 20;
int y = 10;
System.out.println(x / y); 






Q5. In Java, what is the scope of a local variable?






Related Java Variable Topics