Show Mobile Navigation

Webring

Powered by WebRing.
,

How to declare,initialize and use local variables in Java?

Sachin R Kukale - 02:12

In this article we will what is local variable? How they are declared and how can be used in
your program in various conditions?
Local variables are those variables which are declared inside a method, a constructor or a block.
Local variables have very limited scope in Java. The scope of local variables starts when a method or block
including local variable starts executing and these local variables are destroyed when execution of
method of method or block declaring them finishes.
So local variables are attached to the method or block of code in which they are declared. In memory
local variables are stored along with their methods in heap region.
In a method you can define a local variables anywhere between opening and closing braces of method
declaration and they do not have any special syntax or keyword to indicate that they are local. Only
the location of these variables decide whether they static, instance or local variables.
In following examples we will see the code to declare, initialize and using of local variables
in Java.

It is completely illegal to use the uninitialized local variable. You keep a variable
uninitialized but when you try to use that uninitialized variables you will get compiler error.

public class LocalVariable{

public LocalVariable(){
//This is no-argument constructor for LocalVariabl class.

int v=0;

//We can declare local variables inside the constructor also.

//But we can not use them outside constructor.
System.out.println(v+" from constructor.\n");

}

public static void main(String[] args){

LocalVariable lv=new LocalVariable();

lv.print();

lv.useit();

}

public void print(){

int i;
//Just declared but not initialized and it is illegal.

String str="I am local variable.";
//Declared and initialized.

System.out.println(str+" from print method.");

System.out.println("It is impossible to access me from anywhere outside the print method\n");

Child ch=new Child();
//Creating the child class to try to access the local

//variables in child class.

ch.fun();

//System.out.println(i);
//Compiler will complain while using uninitialized variable.
}

public void useit(){

//System.out.println(str);
//You can't use local variable decalred in another method even if you

//you are accessing the local variables in method within same class.


}

Now we will access the local variable declared in method of one class in another class's method.
Which is not possible because scope of local variable ends with the method in which it is declared.

class Child extends LocalVariable{

//System.out.println(str);
//Accessing local variable inside child class will give you compiler error.

void fun(){

System.out.println("fun method called...");

/*System.out.println(str);
Accessing local variable inside the child class's method will also
result in compiler error.*/

}

}

}

It is very simple to understand that as local variables do not have scope beyond the method you can
not use them anywhere outside the method, constructor or block of code where they are declared.
Keep this point always in mind as it asked many times in the interview.

0 comments:

Post a Comment