Variables In Java Programming


In this post, we will look at Variables in Java Programming. It is an essential topic that will provide an insight into how a user stores and handles the data in the computer’s memory to use it efficiently.

So, we will cover everything from the definition to the declaration and the different types of variables in java.

What are variables in java?

In Java, variables are nothing but a named memory location. Variables in java stores values of elements while the program executes. All operations done on the variable affects the memory location.

Simply put, Variables are helpful to programmers to handle data that can be manipulated by the program during execution as well. Variables in java are Strongly typed; hence they all must have a datatype followed by an identifier.

Each variable has a specific Data Type that defines the size and layout of the variable’s memory.

How to Declare a Variable?

We can declare a variable as below:

Here is the example:

variable declaration

💡 Did you know?

Assigning value is optional while declaring variables. You can declare variable and assign value to it later in program.

Variable has a name that is used to identify it. The name of the variable is known as the variable’s identifier.

There are some naming conventions that you need to follow while declaring a variable.

  • You can use A to Z or a to z.
  • You can use 0 to 9.
  • You can use special symbol$ and _ to declare a variable.
  • You can not start a variable name with number.
  • You can not have spaces in variable name.
  • You can not use reserved keywords in variable name.
  • Variables names are case sensitive.


You can declare variables in groups as well like this:

How to Initialize Variables?

Each variable holds some data which it describes. We use = operator to assign value to a variable.

Compiler will give compile time error if value does not match with the datatype of the variable.
For example:
You can not assign String literal to int data type.

Let’s understand it with the help of simple program:

When you run above program, you will get the below Output:

What are the different types of variables in java?

There are majorly three Types of Variables in use in Java Programming.

  1. Local Variable
  2. Instance Variable
  3. Static Variable

Let’s have a look at each type in detail with some examples.

Local variable

A variable declared inside a method or a block is termed a Local Variable. In Java, Local Variables can also be declared in a block within a method surrounded by curly braces; such variables are known as Block Level Local Variables.

In such a case, the scope of the variable, both in terms of its access and the presence of the variable in the memory, will be valid throughout the block only and not through the entire program. We will understand this with an example.

Note: It is mandatory to initialize the local variables; otherwise, the compiler will throw an error about it. Moreover, we cannot have Keywords like public, private, static associated with Local Variables.



Instance variable

A variable declared at the class level but outside a method or a block is an Instance variable. It is not mandatory to initialize instance variables. Instead, an Instance variable in Java is initialized when an instance of the class or an object is created.

All instance variables will be, by default, initialized by JVM. While allocating space for an object, a slot for each Instance Variable is also created. Thus, we can say Instance Variables are present in memory until the object’s lifetime and are visible to all methods and blocks in the class.

We can use Keywords and Access Modifiers with Instance Variables to restrict the visibility of the variables. Let us understand the use of instance variable with example.





To access the value in the main method, we had to create the object or instance of the Class InstanceDemo; then, we used the dot operator with the object name to access the variable.

If we had accessed it directly without creating the object, the compiler would throw an error: non-static variable g cannot be referenced from a static context. This error means that the main method is static, and we cannot access a non-static variable from a static method. It is mandatory to instantiate the class in this case.

Static variable

A variable that is declared as static and refers to shared property for all class objects is known as a Static variable. Static variables are also class-level variables that can be declared in a method but only in a Static Method. We cannot declare static variables inside a Static Block.

Static Variables in Java make the program memory efficient as static variables get memory allocation only once during class loading. In simple words, all objects of a class will share the same static variable or properties.

Even if we create multiple objects or instances, static members are created only once. The static keyword denotes that a property belongs to the whole class rather than a method. Like Instance variables, Static members are also by default initialized by JVM. Let us understand the use of static variables with an example.



Here, we print the values of static variables of class: StaticDemo and Demo. Interesting thing we see that, there is no need to instantiate the class Demo to access its static variable we can access it just using the Class name and dot operator with the variable name.

Use Cases for different Variables in Java

Let’s assume we are trying to store information related to a Student object. Now, we know that each Student’s names and roll number are unique to them and are different, so we can use Instance Variables to store that kind of information.

Suppose we want to store information that will be the same for all Student Objects like their School Name, so in that case, we can use a Static variable so that every object shares the same variable. If we want to temporarily store any value related to the object for any operations, we can use Local Variables.

That’s all about Variables in Java Programming, you can try the above examples in your compiler to have a better understanding.

Was this post helpful?

Related Posts

  • 29 November

    How to Get Variable From Another Class in Java

    Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.