Variables in Java

Variables are used to store values that are used in your Java program. A Java variable is a name to a memory location that contains the value assigned to that variable. In this post we’ll see how to declare and initialize a variable in Java, types of variables in Java and their scope.

Declaring a variable in Java

In Java it is mandatory to declare a variable before use. Syntax for variable declaration in Java is as follows-

Here type can be a primitive data type, or a Class name or interface, variable_name is the name of the variable. Value is a value of a compatible type, “=value” part is optional and required only if you are initializing a variable when declared.

For example-

Here age is the variable name which is of type int and initialized with the value 65.

Same variable declaration and initialization can be done separately too.

Some other examples of variable declaration and initialization.

You can also declare more than one variable of the same type in a single line as a comma-separated variables.

Types of variables in Java

The Java programming language defines the following kinds of variables:

  • Instance Variables (Non-Static Fields)
  • Class Variables (Static Fields)
  • Local Variables
  • Parameters

Instance variable in Java

Instance variables in Java (also known as non-static fields) are those variables which are created for each instance (object) of the class. Each object of the class will have a separate copy of instance variable.
Instance variables are declared in a class not in any method or constructor.

Instance variables Java example


In the example there are three instance variables in class Employee. Then two objects of Employee class are created both have their own copy of instance variables with different values.

Class variables (static variables) in Java

Class variables are also declared in a class not in any method or constructor and these variables also use static keyword.
A static variable is associated with the class and all the instances of the class share the same variable i.e. there is only one copy of static variable.

Static variable Java example


In the example there is a static variable DAYS_PAID which is accessed using the class name (Employee.DAYS_PAID).

Local variables in Java

A variable defined between the start and closing curly braces of a method is known as a local variable. Scope of a local variable is with in the method where it is declared. In fact every block of code creates a new scope and any variable declared with in the opening and closing braces of that block has the scope with in that block.

In the code sum is a local variable having its scope with in the method where it is declared. Another variable temp is declared with in the if statement so its scope is with in the opening and closing braces of the if condition block.

Parameters in Java

Arguments passed to a method are known as parameters. Modifying value of a primitive type parameter in a method doesn’t change the original value.


As you can see x is passed as a method parameter which is modified in the method but it doesn’t change the original value.

Local Variable Type Inference in Java 10

From Java 10 it is possible to declare type of local variable as “var”. Java compiler infers the type of a local variable using the value it is initialized with.

For example

Local Variable Type Inference can be used only with local variables with initializer, indexes in for-each loop, local variables declared in for loop.

That’s all for the topic Variables in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.