Open In App

Java Variables

In Java, Variables are the data containers that save the data values during Java program execution. Every Variable in Java is assigned a data type that designates the type and quantity of value it can hold. A variable is a memory location name for the data.

Variables in Java

Java variable is a name given to a memory location. It is the basic unit of storage in a program.



How to Declare Variables in Java?

We can declare variables in Java as pictorially depicted below as a visual aid.

 

From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are:



  1. datatype: Type of data that can be stored in this variable. 
  2. data_name: Name was given to the variable. 

In this way, a name can only be given to a memory location. It can be assigned values in two ways: 

How to Initialize Variables in Java?

It can be perceived with the help of 3 components that are as follows:

 

Illustrations: 

// Declaring float variable
float simpleInterest;
// Declaring and initializing integer variable
int time = 10, speed = 20;
// Declaring and initializing character variable
char var = 'h';

Types of Variables in Java

Now let us discuss different types of variables  which are listed as follows: 

  1. Local Variables
  2. Instance Variables
  3. Static Variables

 

Let us discuss the traits of every type of variable listed here in detail.

1. Local Variables 

A variable defined within a block or method or constructor is called a local variable. 

Time Complexity of the Method:

Time Complexity: O(1)
Auxiliary Space: O(1)

Below is the implementation of the above approach:




// 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);
    }
}

Output
Local Variable: 10


Example :




package a;
public class LocalVariable {
    public static void main(String[] args)
    {
        // x is a local variable
        int x = 10;
 
        // message is also a local
        // variable
        String message = "Hello, world!";
 
        System.out.println("x = " + x);
        System.out.println("message = " + message);
 
        if (x > 5) {
            // result is a
            // local variable
            String result = "x is greater than 5";
            System.out.println(result);
        }
 
        // Uncommenting the line below will result in a
        // compile-time error System.out.println(result);
 
        for (int i = 0; i < 3; i++) {
            String loopMessage
                = "Iteration "
                  + i; // loopMessage is a local variable
            System.out.println(loopMessage);
        }
 
        // Uncommenting the line below will result in a
        // compile-time error
        // System.out.println(loopMessage);
    }
}

Output :

message = Hello, world!
x is greater than 5
Iteration 0
Iteration 1
Iteration 2

2. Instance Variables

Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block. 

The complexity of the method:

Time Complexity: O(1)
Auxiliary Space: O(1)

Below is the implementation of the above approach:




// 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);
    }
}

Output
Geek name is: Shubham Jain
Default value for int is 0
Default value for Integer is null

3. Static Variables

Static variables are also known as class variables. 

The complexity of the method:

Time Complexity: O(1)
Auxiliary Space: O(1)

Below is the implementation of the above approach:




// 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.
    }
}

Output
Geek Name is : Shubham Jain

Differences Between the Instance Variables and the Static Variables

Now let us discuss the differences between the Instance variables and the Static variables:

Syntax: Static and instance variables

class GFG
{
// Static variable
static int a;

// Instance variable
int b;
}

Conclusion

The Important points to remember in the articles are mentioned below:

FAQs on Variables in Java

Q1. What are variables in Java?

Variables are the containers in Java that can store data values inside them.

Q2. What are the 3 types of variables in Java?

There are three types of variables in Java are mentioned below:

  1. Local Variables
  2. Static Variables
  3. Instance Variables

Q3. How to declare variables in Java examples?

We can declare variables in java with syntax as mentioned below:

data_type variable_name;

Example:

// Integer datatype with var1 name
int var1;

Must Read:


Article Tags :