A variable is simply a name for a memory location where you can store data. This data can be changed (or “varied”) during the execution of your program, which is why they are called variables.
In Java, before you can use a variable, you must first declare it. When declaring a variable, you tell Java two things:
- The data type (e.g., int, String)
- The name of the variable (e.g., age, name)
Example:
int age = 25; // This is an integer variable named age, with the value 25
String name = "John"; // This is a string variable named name, with the value "John"
Here, age
is a variable of type int (which stores whole numbers), and name
is a variable of type String (which stores text).
Types of Variables in Java
Java has three types of variables:
- Local Variables
- Instance Variables (also called fields)
- Static Variables (also called class variables)
1. Local Variables
- Definition: These are variables that are declared inside methods, constructors, or blocks.
- Scope: They can only be used inside the method or block where they are defined.
- Example:
public class Example {
public void show() {
int number = 10; // Local variable inside the show method
System.out.println(number); // Output: 10
}
}
Here, number
is a local variable because it is defined inside the show
method.
2. Instance Variables
- Definition: These are variables declared inside a class but outside any method, constructor, or block.
- Scope: They are available to all methods inside the class and can hold different values for different objects.
- Example:
public class Person {
String name; // Instance variable
public void setName(String newName) {
name = newName;
}
public void printName() {
System.out.println(name); // Output depends on the value of name
}
}
In this example, name
is an instance variable, and it can be accessed by all methods in the Person
class.
3. Static Variables
- Definition: These are variables declared with the static keyword inside a class.
- Scope: They are shared among all instances (objects) of the class, meaning the value is the same for all objects.
- Example:
public class Car {
static String brand = "Toyota"; // Static variable
public void showBrand() {
System.out.println(brand); // Output: Toyota
}
}
Here, brand
is a static variable, meaning all Car
objects will share the same brand
value.
How to Declare and Initialize Variables
In Java, a variable can be declared first and then initialized (given a value) later, or you can do both at once.
- Declaration: You just declare the variable’s type and name.
int age; // Declaration
- Initialization: Assign a value to the declared variable.
age = 25; // Initialization
- Declaration and Initialization Together:
int age = 25; // Both declaration and initialization
Rules for Naming Variables
When creating variables in Java, you must follow these rules:
- Variable names must start with a letter,
$
, or_
(underscore). - Variable names are case-sensitive. This means
age
andAge
are different variables. - Avoid using Java keywords (like
class
,public
,static
, etc.) as variable names.
Good practice:
- Use meaningful names that explain what the variable stores. For example, use
age
instead ofa
.
Types of Data You Can Store
Java supports various data types for variables. Here are some common ones:
- int: Stores whole numbers (e.g.,
25
,100
). - double: Stores decimal numbers (e.g.,
12.34
,9.99
). - char: Stores a single character (e.g.,
'A'
,'b'
). - String: Stores text (e.g.,
"Hello"
,"Java"
). - boolean: Stores true or false values (
true
,false
).
Example:
int age = 30; // Stores a whole number
double salary = 5000.50; // Stores a decimal number
char grade = 'A'; // Stores a single character
String name = "Alice"; // Stores a string (text)
boolean isJavaFun = true; // Stores a boolean value
Variable Scope
The scope of a variable refers to where in your program the variable can be accessed.
- Local variables: Can only be accessed within the method or block they are declared in.
- Instance variables: Can be accessed by any method in the class.
- Static variables: Can be accessed by any method in the class, but are shared by all objects.
Conclusion
Understanding how variables work in Java is essential for writing effective and flexible programs. Whether you are working with numbers, text, or logical values, knowing how to declare, initialize, and use variables is the first step in mastering Java programming.
Here’s a quick recap:
- Variables store data that can change during the program.
- There are three types of variables in Java: local, instance, and static.
- You can store different types of data using different data types like int, String, boolean, and more.
By grasping the basics of Java variables, you’ll be well on your way to writing more advanced Java code. Keep practicing, and you’ll get the hang of it in no time!