Java Variables (original) (raw)

Last Updated : 09 Jun, 2025

In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.

**Key Components of Variables in Java:

A variable in Java has three components, which are listed below:

**Note: There are three types of variables in Java: Local, Instance, and **Static.

**Example of variable declaration:

int age = 25; // Integer variable

String name = "Java"; // String variable

double salary = 50000.50; // Double variable

How to Declare Java Variables?

**The image below demonstrates how we can declare a variable in Java:

Variables in Java

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

  1. **data type: In Java, a data type define the type of data that a variable can hold.
  2. **variable name: Must follow Java naming conventions (e.g., camelCase).

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

How to Initialize Java Variables?

It can be perceived with the help of 3 components explained above:

Java Variables Syntax

**Example:

// 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 Java Variables

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

Types of Variables in Java

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.

**Example: This example show how a **local variable is declared and used inside the main method and it can not be used outside of it.

Java `

// Java Program to show the use of local variables import java.io.*;

class Geeks { 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);
}

}

`

**Example: This example demonstrates that local variables are only accessible within the block in which they are declared

Java `

// Java Program to show the use of // Local Variables import java.io.*;

public class Geeks {

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

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

**2. Instance Variables

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

**Example: This example demonstrates the use of instance variables, which are declared within a class and initialized via a constructor, with default values for uninitialized primitive types.

Java `

// Java Program to show the use of // Instance Variables import java.io.*;

class Geeks {

// Declared Instance Variable
public String geek;
public int i;
public Integer I;
public Geeks()
{
    // Default Constructor
    // initializing Instance Variable
    this.geek = "Sweta Dash";
}

// Main Method
public static void main(String[] args)
{
    // Object Creation
    Geeks name = new Geeks();

    // 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: Sweta Dash Default value for int is 0 Default value for Integer is: null

**3. Static Variables

Static variables are also known as class variables.

**Example: This example demonstrates the use of static variables, which belong to the class and can be accessed without creating an object of the class.

Java `

// Java Program to show the use of // Static variables import java.io.*;

class Geeks {

// Declared static variable
public static String geek = "Sweta Dash";

public static void main(String[] args)
{

    // geek variable can be accessed without object
    // creation Displaying O/P Geeks.geek --> using the
    // static variable
    System.out.println("Geek Name is: " + Geeks.geek);

    // static int c = 0;
    // above line, when uncommented,
    // will throw an error as static variables cannot be
    // declared locally.
}

}

`

Output

Geek Name is: Sweta Dash

Instance Variables vs Static Variables

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

**Syntax: Static and instance variables

class Geeks

{

// Static variable

static int a;

// Instance variable

int b;

}

Common Mistakes to Avoid

The common mistakes that can occur when working with variables in Java are listed below: