Variables in Java
Variables are like labeled boxes where your Java program stores data — names, numbers, anything you need. They’re the first thing you learn in programming because they’re used everywhere. In this post, we’ll break down Java variables, their types, real examples, and answer common beginner questions.
1.Local Variables
Defined inside a method or block. Only exists during that method’s execution.
public class Example {
public void greet() {
String message = "Hello, Java!";
System.out.println(message);
}
}Common doubts:
1. Can I use message in another method?
No, local variables live and die inside the method.
2. Do I need to initialize it?
Yes! Java will throw an error if you try to use an uninitialized local variable.
2. Instance Variables (Non-static or Object Variables)
Defined inside a class but outside any method. Belongs to each object.
public class Dog {
String breed; // Instance variable
public void bark() {
System.out.println("The dog is a " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.breed = "Labrador";
d1.bark(); //The dog is a Labrador
Dog d2 = new Dog();
d2.breed = "Beagle";
d2.bark(); //The dog is a Beagle
}
}Common doubts:
1.Why does each object have its own value?
Because instance variables belong to the object (not shared).
Get Dio Pastina Heartlin B’s stories in your inbox
Join Medium for free to get updates from this writer.
2.What’s the default value if I don’t assign anything?
Default values depend on the type (e.g., null, 0, false).
3.Static Variables (Class Variables)
Defined with static keyword. Belongs to the class, shared across all objects.
public class Student {
static String school = "ABC School"; // static variable
String name;
public void printInfo() {
System.out.println(name + " studies in " + school);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Alice";
Student s2 = new Student();
s2.name = "Bob";
s1.printInfo(); // Alice studies in ABC School
s2.printInfo(); // Bob studies in ABC School
// Change the school
Student.school = "XYZ School";
s1.printInfo(); // Alice studies in XYZ School
s2.printInfo(); // Bob studies in XYZ School
}
}Common doubts:
1.Why did both students get the updated school name?
Because school is static variable, it’s shared across all instances.
2.Can I access it without creating an object?
Yes! You can do Student.school .
4. Final Variables (Constants)
Use final to declare a variable that cannot be changed once assigned.
public class Circle {
final double PI = 3.14159;
public void calculateArea(double radius) {
double area = PI * radius * radius;
System.out.println("Area: " + area);
}
}Common doubts:
1.Can I assign a new value to PI later?
No it’s constant. Java will throw a compilation error.
2.Can I declare it without assigning immediately?
Yes, but you must assign it exactly once, either in a constructor or initializer block.
Conclusion
Understanding variables is the first big step toward mastering Java. Whether it’s local, instance, or static variables each plays a specific role in how your program stores and manages data. By knowing where and how to use them, you write cleaner, more efficient, and bug-free code. As you move forward, keep experimenting and questioning, that’s the best way to build a solid foundation in Java.










