A variable is only a name given to the memory location. The variable contains vary+able means the value can be varied. A variable can hold the value when the java program is executed.
- There are 3 types of variables.
- Local Variable
- Instance Variable
- Static Variable
- A local variable is declared inside the method block is called local variable. Local variables can only be used within that particular method and the other methods don’t even know that the variable really exists.
- The instance variable is declared inside the class but outside the body of the method is called the instance variable.
- A variable which is declared with the static keyword is called static variable. The static variable needs only one type of initialization and it is shared among all the instances of the class.
class VariableTypes { int a=10; //instance variable static int b=20; //static variable void method() { int c=30; //local variable }