Variables in java

Let’s learn variables in java.

Variables in java

Variable is a container which holds value in a java program. It is a name of memory location.

Syntax:

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 = 7; // here number is variable

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 body of a method is called local variable. It is only accessible within body of a method.

Local variable in java cannot be used 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 defined with “static” keyword.


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 the body of the method.

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


Static variable in java

Variable declared as static is called static variable. 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,

ClassName.variableName;

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.

Now let’s see example on types of variables in java.

public class JavaVariablesExample
{
   int a = 70; // instance variable
   static int b = 750; // static variable
   void display()
   {
      int c = 50; // local variable
   }
}


Output:

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


Also read – abstraction in java