Variables in java

Let’s learn variables in java.

Variables in java

Variable is the name given to memory location to store some value. Size of memory depends on data type.


datatype variableName = value;

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,

int number = 5;

Variable naming convention in java

A variable name in java

  1. is case sensitive. Variable ‘number’ is not same as ‘NUMBER’, ‘Number’.
  2. must begin with letter of alphabet or underscore (_) or dollar sign ($).
  3. after alphabet variable name can contain digits from 0 to 9.
  4. space between variable name is not allowed.
  5. can be of any length.
  6. cannot use java keywords.

Valid variable names – EmployeeSalary, Employee_Salary, Employee

Not valid variable names- Employee(Dhoni), Employee Salary, EmployeeSalary&7, 7th_Employee_Salary

Types of variables in java

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

Local variable in java: A variable declared inside a method, block or constructor is called local variable. It is only accessible inside a method.

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

Local variable scope is limited to the method in which it is declared.

Compiler will throw “cannot be resolved to a variable” error if you use local variables ’empAge’ and ‘name’ outside ’employeeDetails method.

public class LocalVariableExample
   public void employeeDetails()
      // local variable empAge and name
      int empAge = 22;
      String name = "Sachin";
   public static void main(String[] args)
      LocalVariableExample obj = new LocalVariableExample();
      // name and empAge cannot 
      // be resolved to a variable
      System.out.println("Employee name is : " + name + ", and age : " + empAge);

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

Instance variable in java

A variable declared inside the class is called instance variable. Value of instance variable are instance specific.

Instance variable is non-static variable used to store state of an object.

Instance variable in java 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 “null”.

public class InstanceVariableDemo
   // instance variable declared inside the class and outside the method
   int c;
   public void subtract()
      int x = 100;
      int y = 50;
      c = x - y;
      System.out.println("Subtraction: " + c);
   public void multiply()
      int m = 10;
      int n = 5;
      c = m * n;
      System.out.println("Multiplication: " + c);
   public static void main(String[] args)
      InstanceVariableDemo obj = new InstanceVariableDemo();


Subtraction: 50
Multiplication: 50

Static variable in java

Static variable in java 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,


Default value of static variable for primitive integers (long, short) is 0; for floating points is 0.0; for ‘boolean’ it is false and for object reference it is null.

Static variables are created within a class. Static variable once created is common to all objects.

Memory allocation for static variable happens once when class is loaded in the memory.

Here’s static variable example in java.

public class StaticVariableExample
   // static variable
   static int a = 0;
   public void add()
   public static void main(String[] args)
      StaticVariableExample obj1 = new StaticVariableExample();
      StaticVariableExample obj2 = new StaticVariableExample();
      // both objects are sharing same copy of static variable
      System.out.println("Object 1 value is: " + a);
      System.out.println("Object 2 value is: " + a);


Object 1 value is: 2
Object 2 value is: 2

Also read – abstraction in java