What are variables in Java?

Variables represent the state of an object. it is a piece of memory that can contain a data. Variables are the collection of characters, which holds a value that vary. The name of a variable is called an identifier and whenever a variable is declared, it is either assigned a value or it holds a default value.

The name of a variable is provided explicitly and a type for each variable and an optional initializer. When you declare more than one variable then use comma to separate identifier. A variable can be initialized dynamically. To give a variable a type and a name, you write a variable declaration like this:

data-type name-of-variable;

Java variable declaration Example

int x;
int y =10;

A variable has scope, which determines its availability in the different sections of the program and its lifetime.
Let us have a look at the types of variables.

Types of Variables

Java provides three types of variables. They are:

  1. Instance Variable
  2. Local Variable
  3. Class or Static Variable

Instance Variable – The instance variable are those variables which are declared inside the class. They are created when the objects are instantiated and are associated with the objects. When the memory is allocated to an object in the heap then, the space is allotted to each instance variable. The instance variable is created when the object is created and is destroyed when the object is destroyed. The instance variable can be accessed directly by calling the variable name inside the class.

Let us have a look on the example:

import java.io.*;
public class Employee {

   // this instance variable is visible to any child class.
   public String name;

   // marks variable is visible in Employee class only.
   private int empid;

   // name variable is assigned in the constructor.
   public Employee (String eName) {
      name = eName;
   }

   // empid variable is assigned a value.
   public void setID(int eEmpid) {
      empid = eEmpid;
   }

   // prints the employee details.
   public void printEmp() {
      System.out.println("name  : " + name );
      System.out.println("empid :" + empid);
   }

   public static void main(String args[]) {
      Employee eOne = new Employee("Ram");
      eOne.setID(100);
      eOne.printEmp();
 }
}

Local Variable – The variables that are declared and used in methods are known as local variables. They are accessible inside the method where they are declared. They are created when the method is called and are destroyed once the method execution finishes. You cannot use access modifiers for the local variables. There is no default value for the local variables so, when the local variables are declared the value is assigned at the time of initialization only.

Let us have a look on the example:

public class Test {
   public void Marks() {
      int marks = 50;
      marks = marks + 7;
      System.out.println("The student marks are : " + marks);
   }

   public static void main(String args[]) {
      Test test = new Test();
      test.Marks();
   }
}

Class Variable or Static variables – The class variables are also known as static variables and are declared using static keyword. The class variables are global to the class and belong to the entire set of objects that class creates. The static variables are stored in Heap area.
Let us have a look on the program:

public class Student {

   // marks variable is a private static variable
   private static int marks;

   // course is a constant
   public static final String course = "MCA ";

   public static void main(String args[]) {
      marks = 100;
      System.out.println(course + "average marks:" + marks);
   }
}

Variable Scope in Java

The scope of variable depends upon where the variable is declared in the program. It decides the availability of variable in the program. The scope of any variable can come under one of the categories mentioned below:

  • Block Scope
    • A program can be divided into number of blocks. A variable, which is declared inside a block, is not available or accessible outside that block. This is called as Block scope for that variable. Nested blocks can be created and variables declared in the outer block can be accessible in the inner block.
  • Method Scope
    • Method scope of variable is same as block scope with one difference that the variables are accessable throught the method includign nested blocks.
  • Class Scope
    • Class Scope of variable differs from method scope because variables having class scope are accessible from any method or block.

Variable naming conventions in Java

In Java variable names may consist of alphabets, digits, and underscore and dollar character.
But it should follow some of the conditions:

  • It should not begin with a digit.
  • The upper case and lower case are distinct.
  • It should not be a keyword.
  • The white space is not allowed.
  • The variable name can be of any length.

Convention

  • It starts with a lower case character.
  • It must be meaningful.