Definition: Local variables are declared within a method, constructor, or block and are only accessible within that specific part of the code.
Scope: Only within the method, constructor, or block in which they are defined. They can’t be accessed outside of it.
Lifetime: Local variables are created when the method or block begins execution and are destroyed when it ends.
Initialization: Must be initialized before they are used, as they do not have default values.
public void showMessage() { int localVar = 5; // local variable System.out.println(localVar); }
Instance Variables
Definition: Instance variables are non-static variables that are declared inside a class but outside of any method, constructor, or block.
Scope: Each object of the class has its own copy of instance variables, and they are accessible from any non-static method within the same class.
Lifetime: They exist as long as the object is in memory (until the object is destroyed).
Initialization: If not explicitly initialized, they get default values based on their data type (e.g., 0 for integers, null for objects).
public class Car { String color; // instance variable int speed; // instance variable
public void displayInfo() { System.out.println("Color: " + color + ", Speed: " + speed); } }
Static Variables (Class Variables)
Definition: Static variables are variables declared with the static keyword inside a class. These variables are shared across all instances of the class.
Scope: They can be accessed from any static or non-static method of the class. They are commonly accessed using the class name itself (e.g., ClassName.staticVariable).
Lifetime: Static variables exist for the duration of the program. They are created when the class is loaded into memory and destroyed when the program stops.
Initialization: If not explicitly initialized, they get default values based on their data type. They can also be initialized using a static block.
Get Harshh🎀’s stories in your inbox
Join Medium for free to get updates from this writer.
public class Company { static String companyName = "Tech Solutions"; // static variable
Certainly! Here’s a simple Java program that demonstrates the use of all types of variables: instance variables, static variables, local variables, and parameters.
public class VariableDemo {
// Instance variable String instanceVariable;
// Static variable static int staticVariable = 0;
// Constructor to initialize the instance variable public VariableDemo(String value) { this.instanceVariable = value; }
// Method to demonstrate local variable and parameter public void showDetails(int number) { // 'number' is a parameter // Local variable String localVariable = "This is a local variable.";
public static void main(String[] args) { // Creating an object of VariableDemo VariableDemo obj1 = new VariableDemo("Object 1");
// Calling method with a parameter obj1.showDetails(10);
// Modifying the static variable VariableDemo.staticVariable = 5;
// Creating another object VariableDemo obj2 = new VariableDemo("Object 2");
// Calling method with a parameter obj2.showDetails(20);
// Displaying static variable from another instance System.out.println("Static Variable accessed from obj2: " + VariableDemo.staticVariable); } }
Explanation of the Code:
Instance Variable:
String instanceVariable; is an instance variable that holds a string specific to each object created from the class.
Static Variable:
static int staticVariable = 0; is a static variable that is shared among all instances of the class. It is initialized to 0.
Local Variable:
String localVariable = "This is a local variable."; is a local variable defined within the showDetails method. It can only be accessed within that method.
Parameter:
public void showDetails(int number) has a parameter number, which is a variable that receives a value when the method is called.
Output
When you run this program, you would get output similar to the following:
Instance Variable: Object 1 Static Variable: 0 Parameter (number): 10 This is a local variable. Instance Variable: Object 2 Static Variable: 5 Parameter (number): 20 This is a local variable. Static Variable accessed from obj2: 5