difference between instance and local variables in java

Asked on March 11, 2015
difference between instance and local variables in java

Replied on March 11, 2015
1- Instance variable are declared inside the class but not within a method.
class Tiger{
private double height = 19.3;
private String breed;
}
2- Local variables are declared within a method.
class AddLol{
int a;
int b = 9;
public int add(){
int total = a + b;
return total;
}
}
3- Local variables must be initialized before use.
class Foo{
public void go(){
int x;
int z = x + 9;
}
}

Replied on March 11, 2015
Instance variables always get a default value. If you don't explicitly assign a value to an instance variable, or you do't call a setter method, the instance variable still has a value!
integers 0
floating points 0.0
Boolean false
references null
Local variables do not get a defaults value. The compiler complains if you try to use a local variable before the variable is initialized.