Java Operators

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources




Java Tutorial TutorialsPoint

Java Variable

Last updated: [22-dec-2025]

Java variables are containers used to store data during the execution of a program. A variable represents a memory location that is used to hold values. By using variables, we can store data in memory, access it when needed, and also change its value during the run-time of the program.

In Java, every variable must have a data type, which defines the type of data the variable can store.

Syntax of Java Variable

It has the following syntax

dataType variableName = value;
 

In this syntax

  • datatype: It specifies the type of data that can be stored
  • variableName: It defines the name of the variable

Examples of Java Variables

int age = 20;
String city = "Delhi";
double salary = 45000.75;
char grade = 'A';
boolean isPassed = true;

Declaring a Variable Without Value

int marks;

Assigning Value Later

marks = 85;

Changing Variable Value

The value of a variable can be changed during program execution.

int score = 50;
score = 75;   // value changed

Types of Variables in Java

Java has three main types of variables:

1. Local Variable

Declared inside a method and used only within that method.

void show() {
    int x = 10;
}

2. Instance Variable

Declared inside a class but outside any method. It is not declared using static.

class Student {
    int id;
}

3. Static Variable

Declared using the static keyword. It is shared among all objects of a class.

class College {
    static String name = "ABC College";
}

Rules for Naming Variables in Java

  • Variable names must start with a letter, _, or $
  • Cannot start with a number
  • Cannot use Java keywords
  • Variable names are case-sensitive
  • Use meaningful names

Conclusion

Java variables are an essential part of programming. They allow us to store, modify, and manage data efficiently during program execution. Understanding variable types, syntax, and rules is the foundation of learning Java programming.