Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Types of Variables in Java: Java Variables Explained

Updated on 23 November, 2022

16.61K+ views
8 min read

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. 

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)

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.

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.

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?

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?

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.

2. How 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.

3. What 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.

RELATED PROGRAMS