Local/Instance/Class variable

By | December 30, 2017
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

Local variable, variable defined inside method, constructor or block.
Instance variable, a variable inside class. It can be instantiated.
Class variable, inside class, but outside method. Must be static. Only one copy per class.

public class MyTest {

    static String str = "abcd";  // classs variable, only one copy

    private String a = "123";   // instance variable, can be copied by instantiation

    public int calculate() {
        int a = 1;  // local variable
        return a;
    }

}