A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/variables-in-java/ below:

Java Variables - GeeksforGeeks

Java Variables

Last Updated : 23 Jul, 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: The below example demonstrates the variable declaration in Java

Java
// Demonstarting how to declare and use a variable in Java

class Geeks {
    public static void main(String[] args) {
        // Declaring and initializing variables
        
        // Integer variable  
        int age = 25;       
        
        // String variable  
        String name = "GeeksforGeeks"; 
        
        // Double variable 
        double salary = 50000.50;     

        // Displaying the values of variables
        System.out.println("Age: " + age);          
        System.out.println("Name: " + name);        
        System.out.println("Salary: " + salary);
    }
}

Output
Age: 25
Name: GeeksforGeeks
Salary: 50000.5
How to Declare Java Variables?

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

Variable Declaration

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:

Variable Initialization

Example: Here, we are initalizing variables of different types like float, int and char.

Java
// Demonstrating how to intialize variables
// of different types in Java

class Geeks{
    public static void main(String[] args) {
        // Declaring and initializing variables
        
        // Initializing float variable
        float si = 5.5f; 
        
        // Initializing integer variables
        int t = 10; 
        int s = 20; 
        
        // Initializing character variable
        char var = 'h';               

        // Displaying the values of the variables
        System.out.println("Simple Interest: " + si);  
        System.out.println("Speed: " + s);                      
        System.out.println("Time: " + t);                        
        System.out.println("Character: " + var);                
    }
}

Output
Simple Interest: 5.5
Speed: 20
Time: 10
Character: h
Types of Java Variables

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

Type of Variable

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

Output
Local Variable: 10

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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4