Variables in Java

variableslocal variablesstatic variablesinstance variables

In the previous blog we learned about Non-Primitive Data Types. If you want to learn more about the Non-Primitive Data Types then visit Non-Primitive Data Types in Java. Everyone is aware of the variables. In general terms, variables are nothing but whose values keep on changing. Coming to the Programming Language, variables are generally used to store the values. When the program is executed, values get stored in the memory locations and each memory location has a memory address that is hexadecimal. Remembering hexadecimal addresses is not an easy job, so some names are given to that memory location that is called variables.

Declaring Variables

The general syntax of declaring variables is: data type followed by the name of the variable.

General Syntax:

datatype variablename;
int intValue;
double doubleValue;

Note: There should be a semicolon(;) at the end of the declaration. Because a semicolon indicates the end of the statement.


Rules for Declaring Variables

  1. Variables should start with the letters and be the sequence of letters or digits.
  2. Variables should not start with the digits nor with any special characters like @, #, etc except underscore(_) and dollar($).
  3. When declaring the variables, the first letter should always be small.
  4. Blank spaces are not allowed in the variable names.
  5. Java keywords like int, char, class, etc. cannot be used for declaring variable names.
  6. Variable names are case-sensitive.

Initializing Variables

After declaration of variables, we must initialize them by using assignment operators. We cannot use the variables that are not initialized. If we do so then the compiler gives the error.

public class InitializingVariableEx {
  public static void main(String args[]) {
    double value;
    System.out.println(value);
  }
}

Output:

error: variable "value" might not have been initialized...
  System.out.println(value);
There are two ways to initialize the variables:
  1. The first way is to assign the declared variable on the left followed by the assignment operator(=) and then the expressions with the values on the right.
  2. e.g.

    int value; value = 90;
  3. The other way is to declare and initialize it in one line.
  4. e.g.

    int value = 90;

Types of Variables

There are three types of variables:

  • Local variables
  • Instance variables
  • Static variables

Local variables

We declare local variables inside the method. The scope of local variables is limited, i.e. inside the method only, which means we can't change their values and cannot access them outside the method. The local variables must be assigned some values while creation, otherwise we will get errors while compiling the program.

Example without assigning the variable:

public class LocalVariableEx {
  public void displayLocalVariable(){
    System.out.println("Inside the method");
    String shortkicks;
    System.out.println("Local variable:"+ shortkicks);
  }
  public static void main(String args[]) {
    LocalVariableEx obj = new LocalVariableEx();
    obj.displayLocalVariable();
  }
}

Output:

error: variable shortkicks might not have been initialized
System.out.println("Local variable:"+ shortkicks);
^

Example with initialization and assignment:

public class LocalVariableEx {
  public void displayLocalVariable(){
    System.out.println("Inside the method");
    String shortkicks = "Welcome to Java Shortkicks";
    System.out.println("Local variable:"+ shortkicks);
  }
  public static void main(String args[]) {
    LocalVariableEx obj = new LocalVariableEx();
    obj.displayLocalVariable();
  }
}

Output:

Inside the method
Local variable:Welcome to Java Shortkicks

Instance variables

The variables that are declared in the class but outside the method or constructor are called instance variables. When we create the object using a new keyword then instance variables are created and instance variables are destroyed when the object is destroyed. They can be accessed directly by calling the variable inside the class.

e.g.

public class InstanceVariableEx {
  String label = "Instance Variable: ";
  int value = 20;
  public static void main(String args[]) {
    InstanceVariableEx obj = new InstanceVariableEx();
    System.out.println(obj.label);
    System.out.println(obj.value);
  }
}

Output:

Instance Variable: 20

We can also directly access the variables by using methods.

e.g.

public class InstanceVariableEx {
  String label = "Instance Variable: ";
  int value = 20;
  public void displayInstanceVariable() {
    System.out.println(label);
    System.out.println(value);
  }
  public static void main(String args[]) {
    InstanceVariableEx obj = new InstanceVariableEx();
    obj.displayInstanceVariable();
  }
}

Output:

Instance Variable: 20

Static Variables

Static variables are also called class variables. Static variables are declared using the static keyword. They are declared outside of the method or constructor in a class. Only one copy is created for each variable in the class and can be used by as many objects as we can. When the program starts, then static variables are created and destroyed when the program stops. Static variables are accessed by using classname.

e.g.

public class StaticVariableEx {
  public static String value = "Static or class variable";
  public static void main(String args[]) {
    StaticVariableEx obj1 = new StaticVariableEx();
    StaticVariableEx obj2 = new StaticVariableEx();
    StaticVariableEx obj3 = new StaticVariableEx();
    System.out.println(obj1.value);
    System.out.println(obj2.value);
    System.out.println(obj3.value);
    System.out.println("Without using object:"+ StaticVariableEx.value);
  }
}

Output:

Static or class variable
Static or class variable
Static or class variable
Without using object:Static or class variable

If we change the value of the static variables then it is affected on each instance of the class.

public class StaticVariableEx {
  public static String value = "Static or class variable";
  public static void main(String args[]) {
    StaticVariableEx obj1 = new StaticVariableEx();
    StaticVariableEx obj2 = new StaticVariableEx();
    StaticVariableEx obj3 = new StaticVariableEx();
    System.out.println(obj1.value);
    System.out.println(obj2.value);
    System.out.println(obj3.value);
    System.out.println("Without using object:"+ StaticVariableEx.value);
    /* Other Object */
    obj1.value = "Class Variable";
    System.out.println(obj2.value);
    System.out.println(obj3.value);
    System.out.println(obj1.value);
    System.out.println("Without using object:"+ StaticVariableEx.value);
  }
}

Output:

Static or class variable
Static or class variable
Static or class variable
Without using object:Static or class variable
Class Variable
Class Variable
Class Variable
Without using object:Class Variable

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.