Learn variables in java

Welcome to flower brackets blog. Here we are going to learn variables in java.

So, what is meant by variable in java??

Variable is name a of reserved memory location to store some value. Based on the variable we create the memory will be allocated when the program is executed.

The size of memory reserved depends on data type. Data type should be mentioned before running java program.

Declaring variable

In java we declare a variable like this. So first, data type. Data type can be integer, float, double, long etc. followed by variable name then its value,

public class VariableExample
   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,

learn variables in java

  1. local variable
  2. instance variable
  3. static variable

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

Also local variable cannot be accessed outside the method in which it is been declared.

public class LocalVariableDemo 
   public static void main(String[] args) 
      int x = 6;
      int y = 8;
      int z = x * y;



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

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 methods declared inside the class. Default value of instance variable is zero, for boolean it is “false” and for object reference it will ne “null”.

Instance variable can have access modifiers like public, private, protected etc., which define visibility of instance variable within the class or outside the class.

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);


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

Static variable : are variables declared inside a class. But here variables are declared with keyword “static”.

Also read – static keyword in java

Static variables are also called “Class variables” used to declare constants. Access modifiers for static variables are public, private, final and static.

Static can be called by a static or non static methods.

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);


Student name Sachin and ID 155668

Related Posts