Variables in Java

Author: SAI K


Core Java Tutorial

Introduction

In Java, variables are fundamental for storing data that can be manipulated by the program. Understanding the different types of variables and their scopes is essential for writing efficient and effective Java code. This chapter covers the basics of variables in Java, how to declare them, and the various types of variables available.

What is a Variable in Java?

A variable is a container that holds a value while the Java program is executed. Each variable is assigned a data type, which determines the kind of values it can hold. In Java, a variable is a name given to a memory location that stores data.

Characteristics of Variables:

Declaring a Variable

In Java, variables must be declared before they can be used. The basic syntax for declaring a variable is as follows:

type identifier [ = value ][, identifier [ = value ] …];

Example Declarations:


int a, b, c; // declares three integers: a, b, and c
int d = 3, e, f = 5; // declares three more integers and initializes d and f
byte z = 22; // initializes z
double pi = 3.14159; // declares and initializes pi
char x = 'x'; // declares and initializes x with the value 'x'

Different Types of Variables in Java

Java defines three types of variables:

  1. Instance Variables (Non-Static Fields)
  2. Class Variables (Static Fields)
  3. Local Variables

1. Instance Variables (Non-Static Fields)

Instance variables are declared inside a class but outside any method. They are unique to each class instance, meaning each object created from the class has its own copy of these variables.


public class Employee {
    int id; // instance variable
    String empName; // instance variable
    int age; // instance variable
}

public class InstanceVariableExample {
    public static void main(String[] args) {
        Employee emp1 = new Employee();
        Employee emp2 = new Employee();

        emp1.id = 101;
        emp1.empName = "John";
        emp1.age = 30;

        emp2.id = 102;
        emp2.empName = "Doe";
        emp2.age = 25;

        System.out.println(emp1.empName + " is " + emp1.age + " years old.");
        System.out.println(emp2.empName + " is " + emp2.age + " years old.");
    }
}

2. Class Variables (Static Fields)

Class variables are declared with the static keyword inside a class but outside any method. They are shared among all instances of the class.


public class Student {
    int rollNo;
    String name;
    static String college = "ABC College"; // static variable

    Student(int r, String n) {
        rollNo = r;
        name = n;
    }
}

public class StaticVariableExample {
    public static void main(String[] args) {
        Student s1 = new Student(101, "John");
        Student s2 = new Student(102, "Doe");

        System.out.println(s1.name + " studies at " + Student.college);
        System.out.println(s2.name + " studies at " + Student.college);
    }
}

3. Local Variables

Local variables are declared inside a method, constructor, or block. They are created when the method, constructor, or block is entered and destroyed when it is exited. Local variables are only accessible within the method, constructor, or block where they are declared.


public class LocalVariableExample {
    public void calculateSum() {
        int sum = 0; // local variable
        for (int i = 1; i <= 10; i++) {
            sum += i;
        }
        System.out.println("Sum of first 10 numbers: " + sum);
    }

    public static void main(String[] args) {
        LocalVariableExample example = new LocalVariableExample();
        example.calculateSum();
    }
}

Variable Naming Conventions in Java

Conclusion

Understanding the different types of variables in Java and how to use them is crucial for effective programming. Local variables are used within methods for temporary storage, instance variables hold data unique to each object, and class variables are shared among all instances of a class. Proper use of these variables helps in writing clear, maintainable, and efficient code.