Variables in java

In this tutorial, we will see introduction to variables, declaration, and types.


Variables in java is used to store values of elements while the program is executed.

Java variables are nothing but memory location. It stores the values which can be manipulated by program. Variables in java are strongly typed hence they all must have a datatype followed by an identifier.

Each variable has the specific data type which defines size and layout of the variable’s memory.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Declaring a variable

We can declare a variable as below:

Here is the example:

variable declaration

💡 Did you know?

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
  • Varibles names are case sensitive.


You can declare variables in groups as well.

Assigning a value to variable

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

Compiler will give compile type error if value is not matched with datatype.
For example:
You can not assign String literal to int data type.

Types of variables

There are three kinds of variables in java.

Local variable

A Variable which is declared inside a method can be termed as Local Variable. It is mandatory to initialize local variable otherwise Compiler will complain about it.

Instance variable

A Variable which is declared at class level can be termed as Instance variable. It is not mandatory to initializeInstance variable.

All instance variable will be by default initialized by JVM.

Static variable

A Variable which is declared as static is known as Static variable. Static variables are class level variables.

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

When you run above program, you will get below output:

Instance variable: 0
Static variable: 20
Method local variable: 10

That’s all about Variables in java.

Related Posts

  • How to end program in java
    08 March

    How to end program in java

    Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program You can use exit() method of System class to end java program. System.exit() causes Java virtual machine to exit and terminate the current process. You can call […]

  • 25 September

    Reference Variable in Java

    In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class or access static member of the […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 17 June

    How to learn Java Programming

    Java is one of the most used programming languages in the world for developing the desktop application and web application.It has completed 22 years in the programming world. In this post, we will see how to learn java programming in depth and in most efficient way. Java has a huge learning curve, it is a […]

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.