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.
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
}
}int count; and immediately try to System.out.println(count);, the compiler will throw an error.camelCase for variables (e.g., userAge, totalScore). Using snake_case or PascalCase for variables is considered highly unprofessional in the Java ecosystem.let x = 5; x = 'hello';. In Java, doing this will completely halt compilation.What is the difference between a local variable and an instance variable?
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).
What does the 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).
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;
}
}
}Test your understanding of Variables with these quick questions.
Question 1
Which of the following is a valid variable declaration in Java?
Question 2
What happens if you try to reassign a variable marked with the `final` keyword?
Question 3
What is the standard naming convention for variables in Java?
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.
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
}
}int count; and immediately try to System.out.println(count);, the compiler will throw an error.camelCase for variables (e.g., userAge, totalScore). Using snake_case or PascalCase for variables is considered highly unprofessional in the Java ecosystem.let x = 5; x = 'hello';. In Java, doing this will completely halt compilation.What is the difference between a local variable and an instance variable?
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).
What does the 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).
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;
}
}
}Test your understanding of Variables with these quick questions.
Question 1
Which of the following is a valid variable declaration in Java?
Question 2
What happens if you try to reassign a variable marked with the `final` keyword?
Question 3
What is the standard naming convention for variables in Java?