Monday 22 February 2016

Java:scope and lifetime of variables



  • Java allows variables to be declared within any block. A block is begun with an opening curly brace and ended by a closing curly brace.
  • A black defines a scope, if you start a new block it is nothing but creating a new scope.
  • A scope determines what objects are visible to other parts of your program. It also determine the lifetime of those objects.

There are two general categories of scopes global and local.

  • The scope defined by a method begins with its opening curly braces. It that method has parameters, they are including within method scope.
  • Variables declared inside the scope are not visible to code designed outside that scope.
  • When you declare a variable within a scope, you are localizing that variable and protecting it from unauthorized access and /or modification.

Scope can be nested, if you create a block of code for each time, you are creating a new nested scope. When this occurs, the outer scope encloses the inner scope. i.e. objects declared in the outer scope will be visible to code within inner scope.

Objects declared within inner scope will not be visible outside it.

Ex:-

Class scope
{

Public static void main (string args [])
{
Int x’
X=10;
If(x= = 10)
{
System.out.println(x+      +y);
X=x*z;
}
Y=100;
System.out.println(x);
}
}


Explanation:-

Variable x is declared at the start of main ()’scope and it is accessed to all subsequent code within main (). Within the if block, y is declared. Y is only visible to other code within its block. Y=100 line gives the complication error.

              Within a block, variables can be declared at any point but are valid only after they are declared. If you declare a variable at the start of a method, it is available to all of the code within that method.


               If you declare a variable at the end of block, it is effectively useless because no code will be access to it.

             // this fragment is wrong

             Count =100;    // can’t use before it is declared.
              Int count;

Variables are created when their scope is entered and destroyed. When their scope is left variables will not hold the value once it has gone out of scope.

Variables are declared within method will not hold their values between call to that method.

          If a variable declaration includes an initializer, then that variable will be reinitialized each time the block in which it is declared is entered.

// demo on lifetime of a variable.

Class lifetime
{
Public static void main (String args [])
{
Int x;
For(x=0, x <3 p="" x="">
{
Int y=-1;
System.out.println (“y is:”+y);
Y=100;
System.out.println ( “ y is now:”+y);
}
}
}

 Output:

Y is: -1
Y is now: 100
Y is: -1
Y is now: 100
Y is: -1
Y is now: 100


You can’t declare a variable to have the same name as the one in outer scope. Java differ from c, c++

Class scopeError
{
Int bar=1;
{                        // creates a new scope
Int bar=2;      // compile time error (bar is already defined
}
}
}

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top