Tuesday, June 23, 2009

Local Variable , Instance Variable and Shadowing

Instance variables and local variables are two types of variables based on variable declaration.

Instance variables are those variables which are declared just below class declaration and are available to all member methods.These are outside any method body.Instance variable are data fields which are unique to each and every object.

For Example:-

public class test{

//Following both are Instance variable of class test ,whenever the class is instantiated these are also initialized
private String name;
private String address;
}

Main points about Instance variables:-

1.We can use any of four access modifiers(public,private,protected,default).
2.Instance variables cannot be either static or abstract.
3.Instance variables can be marked final or transient.
4.Instance variables cannot be marked strictfp,native and synchronized.

Local variables are variables which are declared within a method.This means the variable is initialized within method and also declared within method.Local variable has its scope and lifetime limited to the method.Local variable gets initialized when a method is loaded and destroyed when a method completes.The important thing to remember is that the local variable itself can be passed as argument in other method calls but its scope is limited to its method.Local variables during declaration must be assigned a value otherwise compiler will slap a error that variable not initialized.Local variables life goes inside a stack not on heap.

Note:-Local variables can't ever be referenced in any code outside the method in which it is declared.

For Example:-

//myvalue inside mymethod is a local variable.
public class testlocal{
public void mymethod(){
int myvalue=21;
}
}

Variable Shadowing:-We can declare local variable with the same name as Instance variable.It is known as variable shadowing.These came in often where the local variable is an argument and you wish to assign a value of Instance variable to the local variable.Sometimes you get confused in this(see following):-

public class shadowconfusion{
int age=21;
public void yourage(int age){
age=age; // In this case it becomes difficult to know which value is assigned to which one
}
}

To remove this confusion we use this keyword.this keyword always refers to the object currently running.That is previously written code can be modified as following:-

public class shadowconfusion{
int age=21;
public void yourage(int age){
this.age=age; //Instance variable is assigned a value from parameter.
}
}

Links:-
Home
Why Java Classes are not marked "final"
India Online Latest From India

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP