Scope and Lifetime of variables in java.

 

Scope and Lifetime of variables in java.


Scope of a variable :it refers to in which areas or sections of a program can the variable be accessed .

Or

Scope of a variable it refers region/location/range in the program where the variable is visible/accessible.


Lifetime of a variable: it refers to how long the variable stays alive in memory.
The lifetime of a variable is the time period in which the variable has valid memory.

Or

Lifetime of a variable : it refers to time duration between variable creation and destroy in memory.


There are three types of variables in java:


1) instance variables :


scope of an instance variable is throughout the class except in static methods. 


Lifetime of an instance variable is until the object stays in memory.


2) class variables :


scope of a class variable is throughout the class.


 lifetime of a class variable is until the end of the program or as long as the class is loaded in memory.


3) local variables:


Scope of a local variable iis within the block in which it is declared.  


lifetime of a local variable is until the control leaves the block in which it is declared.


Shortcut summary for all variables:



Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java