Variables
Overview
Variables are containers for storing data values in the computer's RAM. Without variables, your application would have no way to remember user input, track scores, or maintain state.
Java is a Statically Typed language. This means that unlike JavaScript or Python, you absolutely MUST declare the specific type of data a variable will hold (e.g., integer, text, boolean) before you can use it. The compiler locks this type in permanently. If you create a variable to hold numbers, attempting to store text in it will cause a hard compilation error.
Variables in Java generally consist of three components: the Type (what kind of data it holds), the Name (how you refer to it, using camelCase convention), and the Value (the actual data). You can also add modifiers like final to lock a variable so its value can never be changed after initialization.
Syntax
Notice how age can be updated with a new number, but trying to assign a string to it would instantly crash the compiler. The final keyword is heavily used for constant configuration values.
public class Main {
public static void main(String[] args) {
// 1. Declaration and Initialization
int age = 25;
String name = "Alice";
// 2. Declaration first, Assignment later
double salary;
salary = 75000.50;
// 3. Re-assigning a variable
age = 26; // Valid, age is an integer
// age = "Twenty Six"; // ERROR: Incompatible types!
// 4. Constants (Cannot be changed)
final double PI = 3.14159;
// PI = 3.14; // ERROR: Cannot assign a value to final variable
}
}Common Pitfalls
- Forgetting to initialize a variable before using it. In Java, local variables inside methods do NOT receive a default value. If you declare
int count;and immediately try toSystem.out.println(count);, the compiler will throw an error. - Using the wrong naming convention. Java strictly adheres to
camelCasefor variables (e.g.,userAge,totalScore). Usingsnake_caseorPascalCasefor variables is considered highly unprofessional in the Java ecosystem. - Attempting to change the type of a variable dynamically. In JS you can do
let x = 5; x = 'hello';. In Java, doing this will completely halt compilation.
Interview Questions
Local variables are declared inside a method and are destroyed when the method finishes. They MUST be manually initialized before use. Instance variables are declared at the Class level, are tied to an object instance, and are automatically initialized with default values (e.g., 0 for ints, null for objects).
final keyword do when applied to a variable?It makes the variable a constant. Once it is assigned a value, it can never be reassigned. For primitives, the literal value is locked. For objects, the memory reference is locked (though the internal state of the object can still be modified).
Real-World Example
When building a banking application, you must use variables to carefully track the user's account balance, account number, and transaction limits. Using final ensures that strict configuration limits (like maximum withdrawal limits) cannot be accidentally overwritten by rogue code.
public class BankAccount {
// Instance variables maintain state across the app
String accountHolder = "John Doe";
double balance = 1500.00;
// final constant prevents accidental tampering
final double MAX_WITHDRAWAL = 500.00;
public void withdraw(double amount) {
if(amount <= MAX_WITHDRAWAL) {
balance = balance - amount;
}
}
}Check Your Knowledge
Test your understanding of Variables with these quick questions.