In Java all variables must be declared before the use.basic declaration will be like,
Syntax:
‘ DataType VariableName; ‘
Example:
int a;
float b;
Continuous declaration also allowed like ‘int a,b,c;’.
There are three types of variables in java.they are,
- Local Variable.
- Instance Variable.
- Class/Static Variable.
Local Variable:
- Local variables are declared inside the method,constructor or block.
- They are visible only inside the block in which they are declared.
- Local variables do not have a default value so it must be declared and initialized before the first use.
- Access modifiers cannot be implemented for local variables.
- Local variables are destroyed immediately after exits the block.
Example Code:
public class Age {
public void printAge()
{
int ageCount = 0; // ageCount is a Local Variable
ageCount = ageCount + 5;
System.out.println(“Age is “+ageCount);
}
public static void main(String args[])
{
Age object1 = new Age();
object1.printAge();
}
}
Instance Variable:
- Instance variables are declared outside the method and constructor or block.
- Memory will be created in the heap when the instance variable is created.
- Instance variable is created when an object is created using the ‘new’ keyword.
- Access modifiers can be applied to these variables.but however ‘private’ is advisable.If the variables needs to be accessed by the subclasses then the visibility can be modified.
- Instance variables have their default values.So it is not a must to declare and instantiate.Example: int a;
Here the variable ‘a’ holds 0 as its default value.
- Instance variables holds values that must be referenced by more than one method,constructor or block.State should be maintained throughout the class.
- They can be directly called by their name inside the class.Subclasses have to use the qualified name.Example: ‘ObjectName.VariableName’
Example Code:
public class Employee {
public String employeeName; // employeeName and employeeSalary are Instance Variables
public int employeeSalary;
public Employee(String name)
{
employeeName = name;
}
public void setEmployeeValues(int pay)
{
employeeSalary = pay;
System.out.println(“Employee Name is “+employeeName+” and Employee Salary is “+employeeSalary);
}
public static void main(String args[])
{
Employee employee = new Employee(“Manishankar”);
employee.setEmployeeValues(50000);
}
}
Class/Static Variable:
- Class variables are also called as Static variables.Variables are declared with a ‘Static’ keyword outside the method,constructor or block.
- There will be only one copy of the static variable.
- Static variables can have access modifiers.
- Static variables starts when the program starts and destroyed when the program stops.
- Static variables have static memory.
- Static variables can be accessed by calling the variable with its class name.Example: ‘ClassName.VariableName’
Example Code:
public class Test {
// Salary is a private static variable
private static int salary;
// Name is a constant variable
private static final String name = “Manishankar”;
public static void main(String args[])
{
salary = 50000;
System.out.println(“Salary is “+salary+” and Name is “+name);
}
}