Java Variables

 

When learning Java, one of the most important concepts to understand is variables. Variables are the foundation of any programming language because they allow us to store, manipulate, and reuse data throughout our programs.

What is a Variable in Java?

 

A variable in Java is a named storage location in memory that holds a value. Think of a variable as a container or a box with a label. The label (variable name) helps you identify what’s inside (the value).

For example:

int age = 25;

Here:

int → is the data type (integer type).

age → is the variable name.

25 → is the value stored in the variable.

Declaring Variables in Java

 

To declare a variable in Java, you follow this syntax:

dataType variableName = value;

Examples:

int number = 10; // Integer variable
double salary = 45000.5; // Decimal value
String name = “Alice”; // Text data

Rules for Naming Variables

 

Must start with a letter, _ (underscore), or $ (dollar sign).

Cannot start with a digit.

Cannot use Java reserved keywords (like class, int, static).

Variable names are case-sensitive (age and Age are different).

Follow camelCase convention for readability (e.g., studentName, totalMarks).

Types of Variables in Java

 

Java variables can be categorized in different ways:

1. Based on Scope

 

Local Variables

Declared inside methods, constructors, or blocks.

Exist only while the method is executing.

Must be initialized before use.

Example:

public void display() {
  int count = 5; // Local variable
  System.out.println(“Count is: ” + count);
}

2. Instance Variables (Non-Static Fields)

 

Declared inside a class but outside methods.

Belong to an object of the class.

Each object has its own copy of the variable.

Example:

class Student {
  String name; // Instance variable
  int age; // Instance variable
}

3. Static Variables (Class Variables)

 

Declared with the static keyword inside a class.

Belong to the class rather than any object.

Shared by all objects of the class.

Example:

class Student {
  static String school = “Tech High School”; // Static variable
}

2. Based on Data Type

 
  1. Primitive Variables (store simple values)

  • byte, short, int, long → Integer types
  • float, double → Decimal types
  • char → Single character
  • boolean → true/false

      Example:

      int age = 20;
      double price = 99.99;
      boolean isJavaFun = true;

2. Reference Variables (store memory address of objects)

 

Used to refer to objects.

Example: String, arrays, or any custom objects.

Example:

String city = “New York”; // Reference variable
int[] numbers = {1, 2, 3}; // Reference variable

Variable Initialization in Java

 

Variables must be initialized (assigned a value) before being used.

Example:

int x; // Declaration
x = 100; // Initialization
System.out.println(x);

Alternatively:

int y = 200; // Declaration + Initialization

Variable Scope and Lifetime

 

Local Variables → Exist only within the method.

Instance Variables → Exist as long as the object exists.

Static Variables → Exist for the entire program run (class loading to program termination).

Constants in Java (Final Variables)

 

If you don’t want a variable’s value to change, use the final keyword.

Example:

final double PI = 3.14159;

Here, PI is a constant and cannot be modified.

Example Program Using Different Types of Variables

 

public class VariableExample {

  // Instance variable
  String name = “Alice”;

  // Static variable
  static String course = “Java Programming”;

  public void display() {
    // Local variable
    int marks = 90;

    System.out.println(“Name: ” + name);
    System.out.println(“Course: ” + course);
    System.out.println(“Marks: ” + marks);
  }

  public static void main(String[] args) {
    VariableExample student = new VariableExample();
    student.display();
  }
}

Output:

 

Name: Alice
Course: Java Programming
Marks: 90

Best Practices for Using Variables in Java

 

Use meaningful names (e.g., studentAge instead of x).

Always initialize variables before use.

Keep variables’ scope as small as possible for better memory management.

Use final for constants.

Follow camelCase naming convention.

Java Variable Scope and Type Visualization Diagram

Conclusion

 

Variables are essential in Java as they provide a way to store and manipulate data within your programs. By understanding the types of variables (local, instance, static), their scopes, and best practices, you’ll write clean, efficient, and maintainable Java code.

Scroll to Top