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
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:
Post a Comment
Post a Comment