A variable in Java is a container for storing data that can be used and manipulated within a program. Variables have a name, a type, and a value. They are fundamental to Java programming as they allow you to store and manage data dynamically.
How to Declare a Variable in Java
To declare a variable, specify:
- The data type (e.g.,
int
,String
,boolean
). - The variable name (identifier).
Syntax:
data_type variable_name = value;
Example:
int age = 25; // Stores an integer value String name = "John"; // Stores a string boolean isAdult = true; // Stores a boolean value
Rules for Naming Variables
- Variable names must start with a letter,
_
, or$
. - Cannot use reserved keywords (like
class
,int
). - Should be descriptive and follow camelCase convention.
- Java is case-sensitive:
myVar
andMyVar
are different.
Types of Variables in Java
- Local Variables
- Instance Variables
- Static (Class) Variables
1. Local Variables
- Declared inside a method, constructor, or block.
- Accessible only within the scope where declared.
- Must be initialized before use.
Example:
public class Main { public static void main(String[] args) { int localVariable = 10; // Local variable System.out.println(localVariable); } }
2. Instance Variables
- Declared outside methods, but inside the class.
- Belong to an object, and each object has its own copy.
- Automatically initialized to default values (e.g., 0 for integers).
Example:
public class Person { String name; // Instance variable int age; // Instance variable }
3. Static (Class) Variables
- Declared with the keyword
static
. - Shared across all objects of the class.
- Useful for constants or values shared among all instances.
Example:
public class Company { static String companyName = "TechCorp"; // Static variable }
Default Values for Variables
Java assigns default values to instance and static variables when no value is explicitly provided. Local variables do not get default values.
Data Type | Default Value |
---|---|
byte , short , int , long |
0 |
float , double |
0.0 |
char |
‘u0000’ (null char) |
boolean |
false |
String or other objects |
null |
Variable Scope
The scope of a variable determines where it can be accessed in the program:
- Local Scope: Limited to the block or method where declared.
- Instance Scope: Accessible throughout the instance of the class.
- Static Scope: Shared and accessible by all instances of the class.
Examples of Variable Usage
Example 1: Local and Instance Variables
public class Person { String name; // Instance variable public void setName(String newName) { String greeting = "Hello"; // Local variable name = newName; System.out.println(greeting + ", " + name); } public static void main(String[] args) { Person person = new Person(); person.setName("Alice"); } }
Example 2: Static Variables
public class Counter { static int count = 0; // Static variable public Counter() { count++; } public static void main(String[] args) { new Counter(); new Counter(); System.out.println("Number of objects created: " + count); // Output: 2 } }
Final Notes
- Variables must be declared with a type before use.
- Choose appropriate variable types based on the data to optimize memory usage.
- Use meaningful variable names to enhance code readability.