Monday, 26 May 2014

Local Variable vs Instance Variable

What is the difference between instance variable and local variable?


1. instance variables are shared among threads while local variable is not shared

2. Example 
public class Test{private String var1; //    instance variablepublic int var2;       //    instance variable
private void method1(){
String var3;            //      local variable
}}

3. Instance variables are object  level member variables. 
Local variables are defined in a method/block.

No comments:

Post a Comment