Just like other programming languages variable is nothing but the way to store and call some data. A variable is assigned with a data type. Variable is a name of memory location. There are three types of variables in java: local, instance and static. There are two types of data types in Java: primitive and non-primitive. int data=50; There are mainly three types of variables:
Local Variable
Instance Variable
Static Variable
Local Variable:
A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. A local variable cannot be defined with "static" keyword.
Instance Variable:
A variable declared inside the class but outside the body of the method. It is not declared as static. It is called an instance variable because its value is not shared with other instances making it isolated and secure.
Static Variable
Static variable is shared among all the instances. Memory allocation for static variables happens only once when the class is loaded in the memory. Static keyword is used to define the static variable.
Example:
public class A
{
static int m=100;
void method()
{
int n=90; //local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}