Variables in Java
- In Java, variables are used to store data.
- Depending on how they are declared and used, variables can be classified into two main types: static variables and instance variables.
Instance Variables
Instance variables are variables that are declared within a class but outside any method, constructor, or block.
Each instance (object) of a class has its own copy of the instance variables.
These variables are created when an object is instantiated and destroyed when the object is destroyed.
Key Characteristics of Instance Variables
Unique per Object: Each object has its own copy of instance variables.
Declared in a Class: Instance variables are declared inside a class but outside any method or constructor.
Accessed via Object: They are accessed using the object reference.
Example of Instance Variables
class Car {
// Instance variables
String color;
String model;
int year;
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Method
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
// Creating objects of the Car class
Car car1 = new Car("Red", "Toyota", 2020);
Car car2 = new Car("Blue", "Honda", 2018);
// Displaying details of car1
car1.displayDetails();
// Displaying details of car2
car2.displayDetails();
}
}
Explanation:
Declaration: String color, String model, and int year are instance variables of the Car class.
Unique per Object: car1 and car2 are two different objects with their own sets of color, model, and year instance variables.
The output will be:
Car Model: Toyota
Car Color: Red
Car Year: 2020
Car Model: Honda
Car Color: Blue
Car Year: 2018
Static Variables
Static variables, also known as class variables, are variables that are declared with the static keyword.
These variables are shared among all instances (objects) of a class.
A single copy of a static variable is created and shared among all instances at the class level.
Key Characteristics of Static Variables
Shared Among All Objects: All objects of the class share the same copy of the static variable.
Declared with static Keyword: Static variables are declared with the static keyword.
Accessed via Class Name or Object: They can be accessed using the class name or an object reference.
Example of Static Variables
class Car {
// Instance variables
String color;
String model;
int year;
// Static variable
static int numberOfCars;
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
numberOfCars++;
}
// Method
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
// Static method
public static void displayNumberOfCars() {
System.out.println("Total number of cars: " + numberOfCars);
}
}
public class Main {
public static void main(String[] args) {
// Creating objects of the Car class
Car car1 = new Car("Red", "Toyota", 2020);
Car car2 = new Car("Blue", "Honda", 2018);
// Displaying details of car1
car1.displayDetails();
// Displaying details of car2
car2.displayDetails();
// Displaying the total number of cars
Car.displayNumberOfCars();
}
}
Explanation:
Declaration: static int numberOfCars is a static variable of the Car class.
Shared Among All Objects: numberOfCars is shared among all instances of the Car class. Each time a new Car object is created, numberOfCars is incremented by 1.
Access via Class Name: Car.displayNumberOfCars(); is called using the class name.
The output will be:
Car Model: Toyota
Car Color: Red
Car Year: 2020
Car Model: Honda
Car Color: Blue
Car Year: 2018
Total number of cars: 2
Summary
Instance Variables:
Declared inside a class but outside any method or constructor.
Each object has its own copy of instance variables.
Accessed using the object reference.
Static Variables:
Declared with the static keyword.
Shared among all objects of the class.
Accessed using the class name or an object reference.
Difference Between Instance and Static Variables in Java
Feature | Instance Variables | Static Variables |
---|---|---|
Declaration | Inside a class, but outside any method, constructor, or block. | Inside a class with the static keyword. |
Memory Allocation | Memory is allocated when an object is created. | Memory is allocated when the class is loaded. |
Scope | Each object has its own copy of instance variables. | Shared among all instances of the class. |
Access | Accessed using object references. | Accessed using the class name or object reference. |
Lifetime | Exists as long as the object exists. | Exists as long as the class is loaded in memory. |
Use Case | Used when each object needs to maintain a separate state. | Used for common properties shared by all instances. |
Initialization | Initialized when the object is created. | Initialized when the class is loaded. |
Belongs To | Belongs to the object. | Belongs to the class. |
Keyword | No special keyword needed. | Requires the static keyword. |