Blog_Banner_Asset
    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
Views
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.

 

Conclusion

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

Profile

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 Tutorials

View All

Suggested Blogs

Best Jobs in IT without coding
134211
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers & Experienced [2023]
900302
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers & Experienced [2024]
905033
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5020
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag & Attributes in HTML: Features, Uses, Examples
5131
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5051
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5122
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5056
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions & Answers (Freshers & Experienced)
5131
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

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