Variables are labels that express a particular position in memory or Variable is a name of memory location.
While declaring a variable we have to take below precaution
- Use only the characters a-z, A-Z, 0-9, _, and character $.
- A name cannot include the space character.
- Do not begin with a digit.
- A name can be of any realistic length.
- Upper and lower case count as different characters.
- A name cannot be a reserved word (keyword).
- A name must not previously be in utilized in this block of the program.
Example of Variables in Java
/*Integer Data Type*/
byte byte_var=10;
short sort_var=15;
int int_var=10;
long long_var=1000L;
/*Floating point Data Type*/
float float_var=12.0f;
double double_var=1000.0d;
/*Non-Numeric Data Type*/
char ch='a';
boolean bool_var=true;//Default value is false
Types of Variable
There are three types of variables in Java
1. Local Variable
A variable that is declared inside the method is called local variable.
Scope of these variables are within the method only, you can not use these variables outside the scope/method, if you used it will give compilation error.
See the below example of Local variable
package BestJavaTutorial;
class Simple
{
public void display()
{
int a=10;//Local Varibale
System.out.println("Value of a is "+a);
}
public static void main(String args[])
{
Simple obj1=new Simple();
obj1.display();
}
}
run:
Value of a is 10
BUILD SUCCESSFUL (total time: 0 seconds)
2. Instance Variable
A variable that is declared inside the class but outside the method, constructor or any block is called instance variable.
See the below example of Instance variable
package BestJavaTutorial;
public class Employee
{
// this instance variable is visible for any child class.
public String name;
// salary variable is visible in Employee class only.
private double salary;
// The name variable is assigned in the constructor.
public Employee(String empName)
{
name = empName;
}
// The salary variable is assigned a value.
public void setSalary(double empSal)
{
salary = empSal;
}
// This method prints the employee details.
public void printEmpDetails()
{
System.out.println("name : " + name);
System.out.println("salary :" + salary);
}
public static void main(String args[])
{
Employee empOne = new Employee("Sagar");
empOne.setSalary(1000);
empOne.printEmpDetails();
}
}
Output
run:
name : Sagar
salary :1000.0
BUILD SUCCESSFUL (total time: 0 seconds)
3. Static Variable
A variable that is declared as static is called static variable. It cannot be local.
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
Static variables are created in memory location when the program starts and destroyed when the program stops.
Static variables can be accessed by calling with the class name for example ClassName.VariableName.
See the below example of Static variable
package BestJavaTutorial;
class StudentDetails
{
int std_rollno;
String std_name;
static String college_name = "Vartak College";
public static String static_var = "I'm a static variable";
StudentDetails(int r, String n)
{
std_rollno = r;
std_name = n;
}
void display()
{
System.out.println(std_rollno + " " + std_name + " " + college_name);
}
public static void main(String args[]) {
StudentDetails s1 = new StudentDetails(101, "Sagar");
StudentDetails s2 = new StudentDetails(102, "Aarti");
s1.display();
s2.display();
System.out.println(StudentDetails.static_var);//static variable called by class name
}
}
Output
run:
101 Sagar Vartak College
102 Aarti Vartak College
I'm a static variable
BUILD SUCCESSFUL (total time: 0 seconds)