Variables in java

Let’s learn variables in java.

Variables in java

Variable is the name given to reserved memory location to store some value. The size of reserved memory depends on data type. Data type should be mentioned before running java program.

To declare a variable, first write data type; Data type can be integer, float, double, long etc. followed by variable name then its value. Like this,

public class VariableDemo
{
   public static void main(String[] args)
   {
      // declare and initialize integer variable
      int age = 10, houseNo = 64;
      // declare float variable
      float interest;
      // declare and initialize character variable
      char gender = 'm';
   }
}



In java there are three types of variables,

variables in java
  1. local variable
  2. instance variable
  3. static variable

Local variable: variable defined inside a method or constructor of a class. It has visibility inside a method.

Local variable cannot be accessed outside the method in which it is declared.

public class LocalVariableDemo 
{
   public void EmployeeDetails() 
   { 
      // local variable empAge and name 
      int empAge = 22; 
      String name = "Sachin"; 
      System.out.println("Employee name is : " + name + ", and age : " + empAge); 
   }

   public static void main(String[] args) 
   {
      LocalVariableDemo obj = new LocalVariableDemo();
      obj.EmployeeDetails();
   }
}



Output:

Employee name is : Sachin, and age : 22

Compiler will throw “cannot be resolved to a variable” error if you use variable outside a method.

NOTE: local variable cannot be declared as “static”.


Instance variable: is non-static variable used to store state of an object. Instance variables are declared inside a class but outside method or constructor.

It can be accessed by creating objects. Default value of instance variable is zero, for boolean it is “false” and for object reference it is “null”.

class Department 
{ 
   // instance variables 
   int mechDepartment; 
   int csDepartment; 
   int ecDepartment; 
}

public class InstanceVariableDemo 
{
   public static void main(String[] args) 
   {
      Department de = new Department(); 
      de.mechDepartment = 150; 
      de.csDepartment = 50; 
      de.ecDepartment = 70;

      System.out.println("Students in each department - "); 
      System.out.println("Mechanical : " + de.mechDepartment); 
      System.out.println("Computer science : " + de.csDepartment); 
      System.out.println("Electronics : " + de.ecDepartment);
   }
}



Output:

Students in each department –
Mechanical : 150
Computer science : 50
Electronics : 70


Static variable

are variables declared inside a class. Here variables are declared with keyword “static”.

Also read – static keyword in java

Static variables are also called “Class variables”. To access static variables we don’t need to create object. Instead we can access static variable using class name like this,

ClassName.variableName;

Default value of static variable is 0. Static variables are created within a class.

class Student
{ 
   // static variables 
   public static int ID; 
   public static String name = "Sachin"; 
}

public class StaticVariableDemo 
{
   public static void main(String[] args) 
   {
      // accessing static variable without creating object 
      Student.ID = 155668; 
      System.out.println("Student name : " + Student.name + ", and ID : " + Student.ID);
   }
}



Output:

Student name : Sachin, and ID : 155668

NOTE:

  • If you access static variable without class name compiler itself adds class name.
  • Like instance variable if you access static variable using object, a warning message is shown. It won’t cease the program, instead compiler restores object with class name.
  • Memory allocation for static variable happens once when class is loaded into memory.