Variables in java

Let’s learn what are variables in java?

Variables in java

Variables are a way to store information in a computer. Variables defined in a java program can be accessed by a name we give them.

Syntax:

datatype variableName = value;

To define a variable we need to specify the data type, then give our variable a name, and optionally add an expression to initialize the variable with a value. Like this,

int number = 7; // here “number” is variable name

Here the data type is an int, the name is number and the value we are assigning or initializing our variable with is 7. We are declaring a variable of type int with the name number and assigning the value 7 to it.

Let’s see a java program on variables in java.

public class Demo
{
   public static void main(String[] args)
   {
      System.out.println("Hello, Java");

      int a = 10 + 5;
      System.out.println(a);
   }
}

Output:

Hello, Java
15


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