What is a variable?

Variable is the most common term in any programming language. While writing the programs, many a time we need to store data/value in our program. We achieve this by creating variables, Variables are nothing but a memory location or block of memory depending upon the type of data we want to store. We can give some meaningful name to that address and then using the name we access the data multiple times.

Variable Example

int age = 20; // variable "age" holds value 20 as integer

Rules for creating variable names

There are several rules and conventions for naming a variable.

  1. Variable names are case sensitive.
  2. Variable names must start with a letter, or the $ or _ character.
  3. A variable name should not match any java reserved keywords.

Valid Java variable names

int varexample;
int varExample;
int VAR_EXAMPLE;
int _varExample;
int $varExample;
int varExample1;
int varExample_1;
int VAR_EXAMPLE_1;

Type of variables

  1. Local variable
  2. Instance variable
  3. Static variable

Local variable

Variables declared in the body of a method are called local variables. This variable can only be used within the method, no other methods are allowed to access it.

Example

public void doSomething() {

    // Local variables inside the method
    int x = 10;
    int y = 20;

    int sum = x + y;

}

Additional points

  1. Local variables are declared in methods, constructors, or blocks.
  2. Access modifiers cannot be used for local variables. 
  3. Local variables are visible only within the declared method, constructor or block.
  4. Local variables are implemented at the stack level internally.
  5. Local variables must be initialized before use.

Instance variable

Instance variables are declared inside a class but outside of any method, constructor or block. As memory is allocated for objects in the heap, slots are created for each instance variable value. The instance variable is also known as field or property of an object.

Example

public class Person {

    String name; // instance variable
    int age;

}

Addition points

  1. Instance variables are declared in a class, but outside a method, constructor or any block.
  2. When memory is allocated for an object in the heap a slot for each instance variable value is created.
  3. Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed.
  4. Instance variables can be declared in the class level before or after use.
  5. Access modifiers can be given for instance variables.

Static variable

Java static variables belong to classes and are initialized only once at the start of execution. Static variables can be accessed directly by class name and no object is required to access it.

Example

public class Person {

    static int countryCode = 91; // static variable

}

Additional points

  1. Static means to remain constant.
  2. Static variables are also known as a class variable. 
  3. Static variables are created when the program starts and destroyed when the program stops.
  4. Static variable need not be called from an object.
  5. Static variables can be accessed by calling with the class name ClassName.VariableName.