What is a variable?

When we want to store any information, we store it in an address of the computer. Instead of remembering the complex address where we have stored our information, we name that address. The naming of an address is known as variable. Variable is the name of memory location.

Variable Declaration

To define a variable, we need to assign a data type for that variable. Data type defines the kind of value this variable can hold (int, long or String etc.).

Example of variable Declaration

public final int var ;

public : Access Modifier applied to variable

final : Non Access Modifier applied to this variable

int : Data type. It defines kind of value this variable can hold (int in this case)

var : Name of the variable

Variable Initialization

Now we are done with defining a variable, we can initialize the above variable by assigning a value to it. In this case, we assign the variable an integer value.

public final int var = 9;

Rules to declare a Variable

  • Every variable name should start with either alphabets or underscore (_) or dollar ($) symbol.

  • No space is allowed in the variable declarations.

  • Except underscore ( _ ) no special symbol are allowed in the middle of variable declaration

  • Variable name always should exist in the left hand side of assignment operators.

  • Maximum length of variable is 64 characters.

  • No keywords should access variable name.

Note: Actually a variable also can start with ¥,¢, or any other currency sign.

Types of Variables

Variables in Java can be defined anywhere in the code (inside a class, inside a method or as a method argument) and can have different modifiers. Depending on these conditions variables in Java can be divided into four categories.

  1. Instance Variable

  2. Static Variable

  3. Local Variable

  4. Method Parameter

Instance Variable (Non Static Fields)

Instance variables are used by objects to store their states. Variables which are defined without the STATIC keyword and are outside any method declaration are object specific and are known as Instance Variables. Such variables are called instance variables because their values are instance specific and values of these variables are not shared among instances.

Example of Instance variable

Output:

instance variable

instance variable

instance variable

instance variable

Changed Text

instance variable

Class Variable (Static Fields)

Variables which are declared with a static keyword inside a Class (outside any Method) are known as Class variable / Static variable. They are known as Class level variables because values of these variables are not specific to any instance but are common to all instances of a class. Such variables will be shared by all instances of an Object.

Output:

class or static variable

class or static variable

class or static variable

Changed Text

Changed Text

Changed Text

As you can see all three statements displayed the same output irrespective of the instance through which it is being accessed. That is why we can access the static variables without using the objects like this: System.out.println(myClassVar);

Note: A static variable can never be defined inside a method i.e it can never be a local variable.

Local Variables (Method Local)

When a variable is declared inside a Method it is known as Method Local Variable. The scope of local variables is only inside the Method, which means local variables cannot be accessed outside that Method. There are some restrictions on access modifier that can be applied on local variables.

Output:

Calling Method

Inside Method

instance variable

If I hadn’t declared the instance variable and only declared the local variable inside method then the statement System.out.println(obj.myVar); would have thrown compilation error. As you cannot change and access local variables outside the method.

Parameters

Parameters are variables that are passed in Methods. For example, String args[] variables in the main Method is a parameter.

Last updated