0% found this document useful (0 votes)
2 views1 page

University Style Java Variables

In Java, variables are categorized into Local, Instance, and Static variables, each with distinct scopes and lifetimes. Local variables are accessible only within their method or block, instance variables are tied to individual objects, and static variables are shared across all instances of a class. The scope determines where the variable can be accessed, while the lifetime indicates how long it remains in memory.

Uploaded by

namithad115
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

University Style Java Variables

In Java, variables are categorized into Local, Instance, and Static variables, each with distinct scopes and lifetimes. Local variables are accessible only within their method or block, instance variables are tied to individual objects, and static variables are shared across all instances of a class. The scope determines where the variable can be accessed, while the lifetime indicates how long it remains in memory.

Uploaded by

namithad115
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like