Java Variables
A variable is a named container that stores data in memory. In Java, every variable must be declared with a type before use — Java is a statically-typed language. This makes Java safer and faster than dynamically-typed languages like Python.
Declaring and Initializing Variables
// Declaration
int age;
String name;
// Initialization (assign value)
age = 22;
name = "Ankit";
// Declaration + Initialization (most common)
int marks = 85;
double percentage = 89.5;
boolean isActive = true;
char grade = 'A';
String city = "Varanasi";
Types of Variables in Java
1. Local Variables
Declared inside methods or blocks. Accessible only within that scope.
public class Demo {
public void show() {
int x = 10; // local variable
System.out.println(x);
}
// System.out.println(x); // ❌ Error — x not accessible here
}
2. Instance Variables
Declared inside a class but outside methods. Each object gets its own copy.
public class Student {
String name; // instance variable
int age; // instance variable
public Student(String n, int a) {
name = n;
age = a;
}
}
Student s1 = new Student("Ankit", 22);
Student s2 = new Student("Priya", 20);
// s1 and s2 have their own name and age
3. Static Variables (Class Variables)
Declared with the static keyword. Shared by ALL objects of the class.
public class Student {
static String academy = "AI Developers Academy";
String name;
public Student(String n) {
name = n;
}
}
// Both objects share the same academy value
Student s1 = new Student("Ankit");
Student s2 = new Student("Priya");
System.out.println(Student.academy); // AI Developers Academy
Variable Naming Rules
- Can contain letters, digits,
_, and$. - Must begin with a letter,
_, or$(not a digit). - Case-sensitive (
age≠Age). - Cannot use reserved keywords like
int,class,public. - Convention: use camelCase —
studentName,totalMarks. - Constants use UPPER_SNAKE_CASE —
MAX_SIZE.
Constants — final keyword
Use final to declare a constant whose value cannot be changed.
final double PI = 3.14159;
final int MAX_ATTEMPTS = 3;
// PI = 3.14; // ❌ Error — cannot reassign final variable
Type Inference — var keyword (Java 10+)
Java 10 introduced var for local variable type inference.
var name = "Ankit"; // Compiler infers String
var age = 22; // Compiler infers int
var price = 99.99; // Compiler infers double
// Note: var works only for LOCAL variables — not fields or parameters
Real Example — Student Record
public class Student {
// Static (shared by all students)
static String academy = "AI Developers Academy";
// Instance variables
String name;
int age;
String course;
// Constructor
public Student(String name, int age, String course) {
this.name = name;
this.age = age;
this.course = course;
}
public void display() {
// Local variable
String label = "Student";
System.out.println(label + ": " + name);
System.out.println("Age: " + age);
System.out.println("Course: " + course);
System.out.println("Academy: " + academy);
}
public static void main(String[] args) {
Student s = new Student("Ankit", 22, "Python");
s.display();
}
}
static variables are useful for shared data like institute name, base URL, or a global counter. But overuse breaks OOP — use sparingly.
Using uninitialized local variables causes a compile error. Instance and static variables get default values (0, null, false), but local variables do NOT.
Create a class Employee with a static variable companyName, instance variables name and salary, and a method display(). Create two employees and print both. Change companyName and see both change.
Quick Revision
- Java variables are statically typed — declare with type first.
- 3 types: local, instance, static.
- Local — inside methods, no default value.
- Instance — per-object, default value assigned.
- Static — shared by all objects of the class.
- Use
finalfor constants. - Use
var(Java 10+) for local type inference. - camelCase for variables, UPPER_SNAKE_CASE for constants.
Interview Questions
Q: Difference between local, instance, and static variables?
A: Local — declared inside methods, accessible only there, no default value. Instance — declared in class, one copy per object, default value. Static — one copy shared by all objects, default value.
Q: What is the difference between final and static?
A: final means the value cannot be reassigned. static means the variable belongs to the class, not an instance. They can be combined — static final is a constant.
Q: Why must local variables be initialized but instance variables don't need to be?
A: Because instance and static variables get default values (0, null, false) automatically by the JVM. Local variables are temporary and the compiler forces you to initialize them before use to avoid bugs.
Q: What is the default value of an int, String, and boolean instance variable?
A: int → 0, String → null, boolean → false. Object references default to null.
Want to Master Java?
Join our Java course in Varanasi — practical classes and career support at our Sarnath centre.