DEV Community

ABISHEK M
ABISHEK M

Posted on

Types of Variables in Java

1. Introduction

A variable is a named memory location used to store data in a Java program.

For example:

int age = 21;
String name = "Abishek";
double salary = 25000.50;
Enter fullscreen mode Exit fullscreen mode

In this example:

  • age stores an integer value.
  • name stores a String value.
  • salary stores a decimal value.

Variables are important because they allow us to store, access, and modify data while a program is running.

2. Types of Variables in Java

Java has three main types of variables:

  1. Local Variable
  2. Instance Variable
  3. Static Variable

A. Local Variable

A variable declared inside a method, constructor, or block is called a local variable.

Example:

class Student {

    void display() {

        int age = 21;

        System.out.println(age);
    }

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

21
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • age is declared inside the display() method.
  • Therefore, it is a local variable.
  • It can only be accessed within its applicable scope.
  • A local variable does not receive a default value. We must initialize it before using it.

B. Instance Variable

A variable declared inside a class but outside methods, constructors, and blocks, without the static keyword, is called an instance variable.

Example:

class Student {

    String name;
    int age;

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Abishek";
        s1.age = 21;

        System.out.println(s1.name);
        System.out.println(s1.age);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
21
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • name and age are instance variables.
  • They belong to the object.
  • Each object gets its own copy of instance variables.
  • Instance variables receive default values if they are not explicitly initialized.

C. Static Variable

A variable declared inside a class using the static keyword is called a static variable.

Example:

class Student {

    static String college = "ABC College";

    public static void main(String[] args) {

        System.out.println(Student.college);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

ABC College
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • college is a static variable.
  • It belongs to the class rather than individual objects.
  • All objects of the class share the same static variable.
  • We can access it using the class name.

3. Difference Between Local, Instance, and Static Variables

Feature Local Variable Instance Variable Static Variable
Declaration Inside method, constructor, or block Inside class, outside methods Inside class using static
Belongs to Its scope Object Class
Default value No default value Yes Yes
Access Within its scope Through an object Through class name
Shared No No Yes

4. Real-World Example

Consider a Student Management System.

Imagine a college has 100 students.

Each student has:

  • A different name.
  • A different age.
  • The same college name.

We can represent this using Java variables.

class Student {

    String name;                       // Instance variable
    int age;                           // Instance variable

    static String college = "ABC";     // Static variable

    void display() {

        int mark = 90;                 // Local variable

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("College: " + college);
        System.out.println("Mark: " + mark);
    }

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Abishek";
        s1.age = 21;

        s1.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Abishek
Age: 21
College: ABC
Mark: 90
Enter fullscreen mode Exit fullscreen mode

In this example:

  • name and age are instance variables because each student can have different values.
  • college is a static variable because all students share the same college.
  • mark is a local variable because it is declared inside the display() method.

5. Important Points to Remember

  1. Java has three main types of variables: Local, Instance, and Static.
  2. Local variables must be initialized before use.
  3. Instance variables belong to objects.
  4. Static variables belong to classes.
  5. Each object has its own instance variables.
  6. Static variables are shared among objects of the same class.
  7. Local variables are accessible only within their scope.

6. Conclusion

Variables are one of the fundamental concepts in Java programming. They help us store and manage data in a program.

Understanding the differences between local, instance, and static variables is important for learning Object-Oriented Programming (OOP) in Java.

By understanding these three types, we can write better-organized and reusable Java programs.

Top comments (0)