Home > Blog > Explain the Types of Variables in Java - List of Java Variables

Explain the Types of Variables in Java - List of Java Variables

Explain the Types of Variables in Java - List of Java Variables

By Upskill Campus
Published Date:   16th August, 2024 Uploaded By:    Priyanka Yadav
Table of Contents [show]

Instead of using computer programming language, you use labels to represent them. These labels are called variables in programming. This blog will explain these labels and their different types of variables in Java. Understanding variables is crucial if you're interested in coding or want to build a career in computers.

 

Overview of Variables in Java

 

These variables live in your computer's memory and hold different information. Each variable has a specific variable name that tells you what's inside. For example, a variable labeled "age" might hold 30. Importantly, you can only put one type of thing in each box. So, an "age" box would only hold numbers, not letters.
 

Before you can use a variable, you need to create it and give it a label. However, this is called declaring a variable. Storing data in a specific memory location and using a name to reference that memory is a crucial step in organizing and accessing information efficiently. You also tell the computer what kind of stuff you'll put in it (like numbers or letters). Before proceeding with the types of variables in Java, let’s understand how to create or declare variable.

 

How to Create a Variable in Java?

 

You can store different types of items in this computer’s memory, such as variables of numbers, variables of words, or even variables containing other variables.


The Importance of Naming

Giving them meaningful names helps you and others understand what's stored inside without having to open every variable.
 

  • Descriptive Names: Using names like customerAge, productPrice, or firstName clearly indicates the types of Java variables stored.
  • Avoid Generic Names: Names like box1, temp, or x might be convenient for short programs but can be confusing in larger projects.
  • Consistency: Maintain a consistent naming style throughout your code for better readability.


Why Good Naming Matters?

  • Improved Code Understandability: Clear variable names make your code easier to follow, even for others who might work on the project later.
  • Reduced Errors: Accurate names can help prevent mistakes because you'll know exactly what data you're working with.
  • Enhanced Collaboration: If you're working with a team, using meaningful names promotes better communication and understanding.

In short, while starting out, it might seem like a small detail, but choosing good variable names is a fundamental step towards writing clean, efficient, and maintainable code.

 

How to Initialize Java All Variable Types?

 

When you declare a variable, you are reserving memory for it. Initialization is the act of assigning a specific value to this reserved space. However, this can be either immediately upon declaration or at a later time. Moreover, the value of a variable is subject to change during program execution.


Example:

public class VariableTest {

public static void main(String[] args) {

int myInt = 5;

String s;

s = "init";

System.out.println(s);

s = s+5;

System.out.println(s);

}

}

We made a box called "myInt" that can hold whole numbers and put the number 5 inside it. Then, we made another box called "s" that can contain words and put the word "init" in it. We showed what was in the "s" box on the computer screen, then changed what was inside the "s" box to something else.

 

Types of Variables in Java

 

Here, we will provide you a list of Java variables. The checklist is as follows:

  • Local Variables
  • Instance Variables
  • Static Variables

Now, we will elaborate on the types of Java variables in detail. 


#1. Local Variables

A local variable Is a temporary tool you use to complete a specific task. Once the task is finished, the tool is put away.
 

  • Limited Lifespan: Local variables exist only within the specific block of code where they're created. In addition, this block could be a method, a loop, or a set of conditional statements. When the block ends, the variable disappears.
  • Restricted Access: You can only use a local variable within the block where it's declared. Other parts of your program can't see or use it.
  • Mandatory Initialization: Before you can use a local variable, you must give it an initial value. As a result, this is because the computer doesn't automatically assign a value to it.

In brief, local variables create and destroy within a specific part of your code. They are essential for managing data within a particular section of your program. However, they have no impact on the rest of your code.

// Java Program to implement

// Local Variables

import java.io.*;

class GFG {

public static void main(String[] args)

{

// Declared a Local Variable

int var = 10;

// This variable is local to this main method only

System.out.println("Local Variable: " + var);

}

}


#2. Instance Variables

Instance variables are another types of variables in Java. They are personal belongings that each individual copy of an object gets to keep. Unlike shared items, these belongings are unique to each person.
 

  • Belong to Objects: Each object of a class has its own set of instance variables. They are created when the object is born and disappear when the object is destroyed.
  • Flexible Access: You can control who can access and modify these variables using access specifiers (like public, private, protected). If you don't specify, there's a default setting.
  • Optional Initialization: You don't have to fill these boxes right away. They can start with default values based on their type (like 0 for numbers, or nothing for text).
  • Exclusive Access: Only the owner of the object (the object itself) can directly use its instance variables.

In short, Instance variables are like personalized attributes that define the state of an object. In addition, they provide a way to store data specific to each object instance. 


Example of the Types of Variables in Java: 

// Java Program to demonstrate

// Instance Variables

import java.io.*;

class GFG {

// Declared Instance Variable

public String geek;

public int i;

public Integer I;

public GFG()

{

// Default Constructor

// initializing Instance Variable

this.geek = "Shubham Jain";

}

// Main Method

public static void main(String[] args)

{

// Object Creation

GFG name = new GFG();

// Displaying O/P

System.out.println("Geek name is: " + name.geek);

System.out.println("Default value for int is "

+ name.i);

 

// toString() called internally

System.out.println("Default value for Integer is "

+ name.I);

}

}


#3. Static Variables

Static Variables are the least types of variables in Java. It is also called class variables. 
 

  • Belongs to the Class: Unlike instance variables, static variables belong to the class itself, not individual objects. There's only one copy of the static variable, no matter how many objects are created.
  • Long-Lived: Static variables exist for the entire duration of the program, unlike instance variables which are tied to object lifecycles.
  • Direct Access: You can access static variables directly using the class name, without needing to create an object.
  • Shared Value: Any changes made to a static variable affect all objects of the class.

Now, we will share an example of static variables - Java variable types. 
 

// Java Program to demonstrate

// Static variables

import java.io.*;

class GFG {

// Declared static variable

public static String geek = "Shubham Jain";

 

public static void main(String[] args)

{

// geek variable can be accessed without object

// creation Displaying O/P GFG.geek --> using the

// static variable

System.out.println("Geek Name is : " + GFG.geek);

// static int c=0;

// above line,when uncommented,

// will throw an error as static variables cannot be

// declared locally.

}

}

 

Example of Variable in Java

 

Here, we will discuss some common examples of types of variables in Java. Read and go through them. 
 

  1. Basic Understanding of Java Variables

public class A

{

static int m=100;//static variable

void method()

{

int n=90;//local variable

}

public static void main(String args[])

{

int data=50;//instance variable

}

}//end of class


 

  1. Adding Two Numbers

public class Simple{

public static void main(String[] args){

int a=10;

int b=10;

int c=a+b;

System.out.println(c);

}

}
 

  1. Widening

public class Simple{

public static void main(String[] args){

int a=10;

float f=a;

System.out.println(a);

System.out.println(f);

}}

 

Conclusion

 

There are three primary types of variables in Java: local, instance, and static. Each serves a distinct purpose within a program's structure. Local variables are temporary containers for data within specific code blocks. On the other hand, Instance variables belong to individual objects and store data unique to each instance. Meanwhile, Static variables are shared among all objects of a class, providing a common storage area.

 

Frequently Asked Questions

 
Q1. How do you define variables?

Ans. A data item that can assume multiple values during program execution.


Q2. What is a variable in programming?

Ans. A variable in programming can hold different things. Moreover, the contents of the variable can change as your program runs. It's a way for your computer to remember information while it's working on a task.

About the Author

Upskill Campus

UpskillCampus provides career assistance facilities not only with their courses but with their applications from Salary builder to Career assistance, they also help School students with what an individual needs to opt for a better career.

Recommended for you

Leave a comment