A Variable is a name given to memory location or container, which is used to hold value while Java Program is executed.
1>Syntax To Declare Variable In Java :
DataType variableName;
Example :
String nameOfDepartment;
2>Syntax To initialize Variable :
DataType variableName=value;
Example :
String nameOfDepartment="Computer Application";
You can also write in two lines as below :
String nameOfDepartment; //declaration
nameOfDepartment="Computer Application"; //initialization
Types Of Variables In Java :
1>Instance Variable(non-static)
2>Local Variable
3>Static Variable
1>Instance Variable(non-static) :
a class level non-static variable declared inside class , outside method. Its value is instance specific and not share among other instances.
2>Local Variable :
variable declared within method itself , other methods can;t access it , a local variable can not be static.
3> Static Variable :
static variable memory allocation happen only once when class is loaded into memory. we declare it using static keyword , we can create single copy of static variable and static variable can be shared among all instances of class.