View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Types of Variables in Java: Java Variables Explained

By Pavan Vadapalli

Updated on Mar 10, 2025 | 10 min read | 16.9k views

Share:

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 Java 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. 

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.

How Many 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)

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.

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

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.

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

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.

Conclusion

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.

Frequently Asked Questions (FAQs)

1. What are variables in Java?

2. How do we declare variables in Java?

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

4. What Are the 8 Types of Variables in Java?

5. What Is a Variable in OOP?

6. What Are Data Types in Java?

7. What Are the 4 Types of Variables in Java?

8. What Is the Variable Data Type in Java?

9. What Are the Different Types of Variables in Data?

10. Can we declare a static variable inside a method in Java?

11. What happens if a variable is declared but not initialized in Java?

Pavan Vadapalli

Pavan Vadapalli

899 articles published

Get Free Consultation

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program