Syntax: data type variable [ = value][, variable [ = value] ...] ;
Variable in Java is a data container that stores the data values during Java program execution. Every variable is assigned data type which designates the type and quantity of value it can hold.
Variable is a memory location name of the data. The Java variables have mainly three types : Local, Instance and Static.
In order to use a variable in a program you to need to perform 2 steps:
To declare a variable, you must specify the data type & give the variable a unique name.
int a, b, c ;
float pi;
double radius;
char a;
String name;
To initialize a variable, you must assign it a valid value.
int a=2, b=5 ,c=6;
float pi = 3.14f;
double area = 20.25d;
char a = ‘v’;
String name=”Java”;
In Java, there are three types of variables:
Local Variables are a variable that are declared inside the body of a method.
Instance variables are defined without the STATIC keyword .They are defined Outside a method declaration. They are Object specific and are known as instance variables.
Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.
class Test{
static int a = 10; //static variable
int b = 20; //instace variable
void method(){
int c = 30; // local variable
}
}