Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconTypes of Variables in Java: Java Variables Explained

Types of Variables in Java: Java Variables Explained

Last updated:
23rd Dec, 2021
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
Types of Variables in Java: Java Variables Explained

In a computer program, data is not stored directly but in numerical or names that help identify the data at various stages of programming. These numerical names that store data in a computer program are referred to as variables in Java language. This blog will help you understand the concept of variables in Java and its various types. If you have a keen interest in coding or programming or want to build a career in computer science, learning about different variables in Java will be helpful for you. 

Learn software development courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

What are variables?

Variables are integral storage components in Java. They can be understood as memory locations. A variable can either be a single memory location or a combination of multiple locations that form a unit. Variables are used to identify the location of data in a program. They do not define the data itself but rather inform the programmer where particular data is located in the memory.

Check out our free courses to get an edge over the competition. 

Ads of upGrad blog

Simply put, variables in Java language are the names used to refer to data stored in the memory. One Java variable can hold only a single type of data. Before using a variable while programming, it is necessary to declare a variable. It means to assign data to a particular memory and use a name for that memory. For declaration, we also need to specify the data type in Java. It is done through the syntax “dataType variableName.”

You can also understand variables as boxes or containers that hold a value during the execution of a computer program and allow the programmer to quickly refer to that value at a later stage.

Types of Variables in Java

Following are the three main types of variables in Java.

1. Local Variables

These variables are declared in methods, constructors, or blocks and are used only inside that particular method or block. You cannot access a local variable outside the method. In Java, methods are described under curly brackets. The area ({….}) between the brackets is called a block or a method.

It defines the scope of local variables. The local variables are used only when the program is running. It is not possible to access a local variable from outside. A local variable starts functioning inside the block or method, but it gets destroyed when it exits the block. It is pertinent to remember that a local variable cannot have a default value. Also, we cannot use access modifiers to declare local variables.

Check out upGrad’s Advanced Certification in DevOps 

2. Instance Variables

Class and subroutines are also essential components in Java. Class means a group of objects that have similar properties. It helps in defining objects, data types, and methods. A class is further divided into subroutines or methods. As you must have understood above, local variables are a part of a subroutine or methods. However, some variables are not a part of the subroutine but directly form a part of the class. Therefore, they are called member variables or instance variables. They have default values. We can declare an instance variable in a class before or after use. All types of instance variables, except for private, are visible within all methods or constructors in a class.

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

Explore Our Software Development Free Courses

Member variables can be both static and nonstatic. We need static, public, or private modifiers to declare an instance variable.

Here is how you can understand the difference between local variables and instance variables. The former is situated within a subroutine or a method within the class, whereas the latter is placed within a class but outside the block or method.

Local variables only exist within a particular subroutine and do not function beyond that. They can only function when that particular subroutine is executed. Contrary to that, instance variables can form a part of several subroutines. Even if the value of an instance variable is situated in one subroutine, it can be used while executing another subroutine.

3. Static Variables

Static is a reference keyword in Java language attached as a prefix to variables, programs, and strings. It is used for memory management in Java. Whenever the word static is added, it indicates that a component belongs to the entire class and not just an object. The term static variable refers to the common property of all objects.

All the objects in a class share a single copy of the static variable amongst themselves. Therefore, they are also called global variables or class variables. The memory allocation for static variables occurs once when the programmer loads the entire class in the memory.

Static variables can be declared only as members of a class and not as a method or subroutine. Thus, they are used only within the class scope in which they are defined and not outside the class.

In addition to the above, another type of secondary variable in Java has been explained below.

4. Final Variables

Static variables can be used as constants in Java by adding the keyword final as a prefix. Those are called final variables. As the name suggests, a final variable’s value becomes final and cannot be modified. However, it is possible to change data within the object.

We cannot override a final variable by any subclass. Also, once a variable becomes final, it cannot be inherited by any class or extended by a subclass. If the programmer tries to change the value of a final variable, a compilation error occurs. Hence, it is essential to initialize a variable before making it final. Initialization can be done at the time of declaration of the variable. Otherwise, it is called a final blank variable.

Explore our Popular Software Engineering Courses

What are Declaring Variables in Java?

We cannot use a variable unless it is declared. The process of creating a variable in Java is known as declaring a variable. However, before creating a variable, it is vital to feed the data type. On creating a variable, some memory units are set aside. That memory location is then identified with the name of the variable.

Therefore, before declaring the name, it is essential to feed the datatype to keep enough memory bits aside to store the variable’s value. The process of variable declaration in Java starts with entering the data type. It is followed by leaving a space and then entering the name of the variable. We can declare variables at any place in the subroutine.

Following are some of the rules that you must keep in mind while declaring a Java variable.

  • You can start the variable’s name with an alphabet, dollar or underscore symbol, or a currency symbol, not any other special symbol.
  • The name of a variable cannot exceed 64 characters.
  • While declaring a variable, you cannot use blank spaces.
  • We cannot use reserved keywords in Java as the name of variables.
  • The name of the variable shall exist on the left side of the assignment operators.

In-Demand Software Development Skills

What is Initialization of Variables in Java?

After the declaration of a variable, an initial value is also assigned to it that can be changed later. This process of assigning an initial value to a variable is called the initialization of variables in Java. Initialization happens before the variable is used within a method. We cannot use a variable if no value is assigned to it. Initialization of a variable can be done right after it is declared. After declaration, if the initialization of variables is not done, an assignment statement is used to provide a value to the variable. An error will occur if you request to print the variable without initializing it.

Initialization of variables is of two types – explicit and implicit. Explicit initialization occurs when a variable is assigned a value at the time of declaration. However, if the variable is given a value later while processing, it is called implicit initialization; if you do not initialize a variable, the system will automatically put a random value while executing the program. It will generate an unexpected outcome.



upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Ads of upGrad blog


In-depth knowledge of Java is quintessential if you want to pursue a career in software development. To know about types of variables in Java and other concepts in detail, you can enrol in the Executive PG Programme in Software Development – Specialisation in Full Stack Development by upGrad offered in association with IIIT Bangalore. This course will help you learn essential skills like building websites, backends, and Web UI by working on many projects and practical assignments. After completing this course, you will be able to apply to various job opportunities like full-stack developer, frontend developer, UI developer, and backend developer.

Read our Popular Articles related to Software Development


Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Frequently Asked Questions (FAQs)

1What are variables in Java?

Variables in Java are containers or boxes that store information or data. They act as memory locations in Java and are the primary storage units. Variables are given specific names before use.

2How do we declare variables in Java?

Creating a variable is called a declaration in Java. It is done by first feeding the data type, then entering a space, and lastly, entering the variable’s name. There are various rules that you must follow at the time of declaration of variables.

3What are the three variables in a class in Java?

There are three main types of variables in Java. Location variables are placed inside a method and member or instance variables that are located in a class but outside a method. Last is the class or static variables that have similar properties.

Explore Free Courses

Suggested Blogs

What is Composition in Java With Examples
Java is a versatile language that supports object-oriented programming and code reusability with building relationships between two classes. There are
Read More

by Arjun Mathur

19 Feb 2024

Software Engineer / Developer Salary in India in 2024 [For Freshers & Experienced]
Summary: In this article, you will learn about Software Engineer Salary in India based on Location, Skills, Experience, country and more. Today softw
Read More

by Rohan Vats

Top 40 Most Common CSS Interview Questions and Answers [For Freshers & Experienced]
Every industry has started the use of websites and applications to catch up with the pace of the rapidly transforming world. CSS is one of the most cr
Read More

by Rohan Vats

19 Feb 2024

Top 10 Highest Paying Programming Languages In India [2024]
Programmers and Developers are among the highest-paid professionals in the world. They earn six-figure annual salary packages in countries like the US
Read More

by Rohan Vats

19 Feb 2024

Top 12 Highest Paying Online Software Courses in India [2024]
The demand for innovative software products and solutions is at an all-time high, and rest assured, it will only peak higher in the coming years. One
Read More

by Rohan Vats

19 Feb 2024

23 Exciting Software Development Project Ideas & Topics for Beginners [updated 2024]
Summary: In this article, you will learn the 23 Exciting Software Development Project Ideas & Topics for 2023. Take a glimpse below. Android tas
Read More

by Rohan Vats

19 Feb 2024

Must Read 50 OOPs Interview Questions & Answers For Freshers & Experienced [2024]
Attending a programming interview and wondering what are all the OOP interview questions and discussions you will go through? Before attending an inte
Read More

by Rohan Vats

18 Feb 2024

25 Exciting Best Django Project Ideas & Topics For Beginners [2023]
What is a Django Project? Django projects with source code are a collection of configurations and files that help with the development of website app
Read More

by Kechit Goyal

18 Feb 2024

44 Must Know Agile Methodology Interview Questions & Answers: Ultimate Guide 2024
Attending an agile interview and wondering what are all the questions and discussions you will go through? Before attending an agile testing interview
Read More

by Arjun Mathur

18 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
footer sticky close icon