JAVA PROGRAMMING
Explain Different Types of Variables, Scope, and
Lifetime in Java (10 Marks)
Introduction: Variables are memory locations used to store data. In Java, variables are classified
into Local Variables, Instance Variables, and Static Variables.
1. Local Variable
Declared inside a method, constructor, or block. It can be accessed only within that method or
block.
2. Instance Variable
Declared inside a class but outside methods. Each object has its own copy of the variable.
3. Static Variable (Class Variable)
Declared using the static keyword. Shared among all objects of the class.
Scope and Lifetime Table
Variable Type Scope Lifetime
Local Variable Within method/block Till method execution ends
Instance Variable Entire class through object Till object exists
Static Variable Entire class Till program terminates
Neat Diagram
VARIABLES IN JAVA | ----------------------------------------- | | | Local Variable Instance Variable Static
Variable | | | Scope: Method Scope: Class Scope: Class Lifetime: End Lifetime: Object Lifetime:
Program of Method Exists Ends
Conclusion: Java variables are categorized into Local, Instance, and Static variables. Their scope
defines where they can be accessed, while their lifetime defines how long they remain in memory.