Skip to content

Variable

Introduction to Variables

A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.

Variable is a name of memory location. There are three types of variables in java: local, instance and static.

There are two types of data types in Java: primitive and non-primitive.

Types of Variables in Java

There are three types of variables in Java:

local variable:
Defined within a method, constructor, or block.
Only accessible within the scope where it's declared.
Not initialized automatically, so must be explicitly initialized before use.

instance variable:
Declared within a class but outside any method.
Each object of the class has its own copy.
Initialized automatically to default values if not explicitly initialized.

static variable:
Declared with the static keyword within a class.
Shared across all instances of the class.
Initialized automatically and only once when the class is loaded.

Short Video Lecture

(Video here)

VisualCodeChat Tutoring (Exercise: Swap Variables)

Interact and Learn with VisualCodeChat!

After-class Exercises

(Exercises here)