Variables In Java With Best Explanation and Examples

In this tutorial, we’ll talk about Variables In Java, how they are used in programs and the different Variable Types In Java that can be useful in your development programs. I will also give you some simple examples to help you understand this topic.

What Is Variables In Java

The Variables In Java is a data container that saves the data values while the Java program is running. Every variable is given a data type that tells what kind and how many values it can hold. A variable is a name for a place in memory where the data is kept.

The Variable In Java is a name for a place in memory. It is the basic way that a program stores information.

  • During program execution, the value stored in a variable can be changed.
  • A variable is just a name for a memory location. All of the operations done on the variable affect that memory location.
  • All variables must be declared in Java before they can be used.

How To Declare Variables In Java

Python Tutorial 2 - Variables
Python Tutorial 2 - Variables

As an example, the picture below shows how we can declare variables in Java.

How To Declare Variables In Java
How To Declare Variables In Java

From the image, it’s clear that when declaring a variable, we need to pay attention to two things:

  • datatype – Type of data that can be saved or stored in this variable.
  • data_name – The name given to the variable.

In this way, only a memory location can be given a name. There are two ways to give it a value:

  • Variable Initialization
  • Giving value by taking input

How To Initialize Variables

Variables are explicitly initialized if they are given a value in the declaration statement. During processing, if a value is given to a variable, this is called implicit initialization.

It can be understood with the help of three things, which are:

  • datatype – Type of data that can be saved or stored in this variable.
  • data_name – The name given to the variable.
  • value – It is the value that the variable was set to at first.
How To Initialize Variables
How To Initialize Variables

Illustrations

float sampleInterest;
// Declaring float variable

int number = 30, speed = 50;
// Declaring and initializing integer variable

char var = ‘g’;
// Declaring and initializing character variable

Types Of Variables In Java

Now let’s talk about the different types of variables, which are as follows:

  • Local Variables
  • Instance Variables
  • Static Variables

Here is an image of what the 3 Variables look like.

Types of Variables in Java
Types of Variables in Java

Local Variables In Java

The Local variables In Java are made when you enter a method, constructor, or block. When you leave a method, constructor, or block, the variable is destroyed. Local variables can’t use access modifiers. Local variables can only be seen inside the method, constructor, or block where they were declared.

Example Of Local Variables Program

/*package whatever //do not write package name here */
// Contributed by Glenn Azuelo
import java.io.*;

public class pies {
    public static void main(String[] args)
    {
        int variable = 26; // Declared a Local Variable
        // This variable is local to this main method only
        System.out.println("Local Variable: " + variable);
    }
}
Output

Local Variable: 26


In order for you to test your Java code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an online compiler in Java for you to test your java code for free.

We also put the Java online compiler below. Just scroll down so that you can easily test or execute the source code in order to learn more about Java Programming Language.

Instance Variables In Java

The Instance Variables In Java, these are non-static variables that are defined outside of any method, constructor, or block. Each object of the class that is created has its own copy, or instance, of that variable. A class contains instance variables.

  • As instance variables are declared in a class, they are created when an object of the class is created and destroyed when the object is destroyed.
  • For instance variables, we can use access specifiers, which we can’t do with local variables. If we don’t give a specific access specifier, the default one will be used.
  • An instance variable does not have to be initialized. Its default value is 0.
  • You can only get to instance variables by making objects.

Example Of Instance Variables Program

/*package whatever //do not write package name here */
// Written By Glenn Magada Azuelo

import java.io.*;

public class PIES {

    public String itsc; // Declared Instance Variable

    public PIES()
    { // Default Constructor

        this.itsc = "Glenn Magada Azuelo"; // initializing Instance Variable
    }
//Main Method
    public static void main(String[] args)
    {

        // Object Creation
        PIES name = new PIES();
        // Displaying O/P
        System.out.println("Programmer name is: " + name.itsc);
    }
}
Output

Programmer name is: Glenn Magada Azuelo

Static Variables In Java

The Static Variables In Java are also called class variables. That is, they belong to a class, not a particular instance. As a result, class initialization will set up static variables. In contrast, a class’s instance will start the instance variables (non-static variables).

  • Like instance variables, these variables are also declared in the same way. The difference is that static variables are declared using the keyword “static” outside of any method, constructor, or block.
  • Unlike instance variables, we can only have one copy of each static variable per class, no matter how many objects we create.
  • Static variables are made when a program starts to run and are destroyed automatically when the program finishes running.
  • If we use an object to access a static variable like an instance variable, the compiler will show a warning message, but the program won’t stop. The compiler will automatically change the name of the object to the name of the class.
  • If we try to use a static variable without the class name, the compiler will add it for us.

Example Of Static Variables Program

/*package whatever //do not write package name here */
// Glenn Magada Azuelo
import java.io.*;

public class PIES {

public static String itsc = "Glenn Magada Azuelo";		 //Declared static variable

    public static void main (String[] args) {
        
    //itsc variable can be accessed without object creation
    //Displaying O/P
    //PIES.itsc --> using the static variable
        System.out.println("Programmer Name is : "+PIES.itsc);
    }
}
Output

Programmer Name is : Glenn Magada Azuelo


Differences Between The Instance Variables And The Static Variables

The Differences Between The Instance Variables And The Static Variables is that when the keyword “new” is used to construct an object, Instance Variables are generated. They are deleted when the object is destroyed. When a program begins, Static Variables are created, and they are deleted when it ends. By calling the variable name within the class, it is possible to access instance variables directly.

Now I’ll talk more about how the two are different.

  • An Instance Variable will have its own copy for each object, but we can only have one copy of a static variable per class, no matter how many objects we make.
  • Because each object has its own copy of an Instance Variable, changes made to an instance variable by one object won’t affect other objects. Static Variables are shared by all objects in a class, so when they are changed, the changes show up in other objects as well.
  • We can get to instance variables by referring to them as objects, and we can get to static variables by using the class name.

Basic Syntax Of Static And Instance Variables

class PIES
{
    // Static variable
    static int number; 
    
    // Instance variable
    int number;        
} 

Conclusion

In this article, we talked about Variables In Java, how it works, and how this chapter could help you learn Java. This could be one of the most important learning guides that will help you improve your skills and knowledge as a future Java Developer.

What’s Next

The next section talks about Modifier In Java programming. At the end of the session, you’ll know what Modifier all about in Java.


Leave a Comment