Types of Variables
1) local variable
2) instance variable
3) class/static variable
Different between local variable and instance variable and static variable
What is instance variable in Java?
- Instance variables in Java are non-static variables which are defined in a class outside any method, constructor or a block. Each instantiated object of the class has a separate copy or instance of that variable. An instance variable belongs to a class.
- An instance in Java is an object created from a class, along with 'new' keyword followed by the class constructor.
SAMPLE PROGRAM :
public class Car {
// Instance variables
String brand;
String model;
int year;
// Constructor to initialize instance variables
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
// Method to display car details
public void displayCarDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
public static void main(String[] args) {
// Create two Car objects (instances)
Car car1 = new Car("Toyota", "Camry", 2022);
Car car2 = new Car("Honda", "Civic", 2023);
System.out.println("Details for Car 1:");
car1.displayCarDetails();
System.out.println("\nDetails for Car 2:");
car2.displayCarDetails();
}
}
OUTPUT:
Brand : Toyota
Model : Camry
year : 2022
Brand : Honda
Model : Civic
year : 2023
program example:
* brand, model, and year are instance variables of the Car class.
* When car1 is created, it gets its own brand, model, and year variables, initialized to "Toyota", "Camry", and 2022 respectively.
* Similarly, car2 gets its own separate set of brand, model, and year variables, initialized to "Honda", "Civic", and 2023.
* Changes made to the instance variables of car1 do not affect the instance variables of car2, demonstrating that each object maintains its own unique state.
LOCAL VARIABLE:
- A local variable in Java is declared within a method, constructor, or block. Its scope is limited to that specific block of code, meaning it can only be accessed from within where it is declared.
- A variable declared inside a method is called a local variable.
- Its scope is limited to the method in which it is declared.
- It cannot be accessed outside that method.
PROGRAM FOR LOCAL VARIABLE:
public class LocalVariableExample {
public void displayMessage() {
// 'message' is a local variable to the displayMessage() method
String message = "Hello from a local variable!";
System.out.println(message);
}
public static void main(String[] args) {
LocalVariableExample obj = new LocalVariableExample();
obj.displayMessage();
// The following line would cause a compile-time error
// because 'message' is out of scope here.
// System.out.println(message);
}
}
OUTPUT:
Hello from a local variable
In this example:
` * The variable message is declared inside the displayMessage() method.
- It is accessible and can be used within the displayMessage() method.
- Attempting to access message from outside the displayMessage() method (e.g., in the main method as shown in the commented-out line) would result in a compile-time error because message is a local variable and its scope is limited to the displayMessage() method.
*Difference between the instance variable vs local variable:
*
Static Variables
* In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we use the static variable most of the time:
- Storing constant values or default values that can be used across different instances. The Static variable is used to store the data of class instances. To record the number of times a class instance is created.
_Important points:
_
-
Class-Level: We can create static variables at the class level only. To know more, refer here.
- Shared Among Objects: The static blocks and static variables are executed in the order they are present in a program. It means if a static variable is modified by any instance, the changes will show in all other instances.
- Accessed through Class Name: Static variables can be called directly with the help of a class only; we do not need to create an object for the class in this.
- Initialization Order :The static variables are initialized before the static blocks, and when the static method or block refers to a static variable, then it will use its initialized value.
Example:
This example demonstrates how to use a static variable among different methods
// Java program to demonstrate execution
// of static blocks and variables
class Geeks
{
// static variable
static int a = m1();
// static block
static
{
System.out.println("Inside static block");
}
// static method
static int m1()
{
System.out.println("from m1");
return 20;
}
// static method(main)
public static void main(String[] args)
{
System.out.println("Value of a : " + a);
System.out.println("from main");
}
}
Output
from m1
Inside static block
Value of a : 20
from main
REFERED LINKS :
Top comments (0)