Variables are named locations in memory that hold values that may be modified by the program. A variable can take different values at different times during execution.
Variables are named storage locations in memory used to hold values.
Every variable has a data type that decides the kind of values it can store (int, float, String, etc.).
They can take different values at different times during execution.
A variable must be initialized before using it, otherwise the compiler will throw an error.
DataType identifier = value; // value is optional
Here, DataType specifies the type of value (e.g., int, float), identifier is the variable name, and value is an optional initialization.
int a; // declaration only
int x = 100; // declaration + initialization
float f = 2.3f; // float requires 'f' at the end
If a variable is declared but not initialized, it may have a default value (for instance & static variables) or cause an error (for local variables).
A local variable is declared inside a method or block and its scope is limited to that block.
They donโt get default values and must be initialized before use.
A non-static variable declared inside a class but outside methods is called an instance variable. Each object gets its own copy of instance variables.
They are automatically initialized with default values (e.g., 0 for int, null for objects).
A static variable is declared with the static keyword inside a class.
It belongs to the class rather than any object.
Static variables cannot be local.
Since only one copy exists per class, changing it affects all objects.
public class Test {
int x = 50; // instance variable
static int y = 100; // static variable
void myMethod() {
int z = 150; // local variable
}
}
Here: x is tied to an object, y is tied to the class, and z exists only during method execution.
Local variables must be initialized before use, while instance and static variables get default values.
In Java, instance and class (static) variables are automatically initialized with default values if not explicitly assigned. Local variables do not have default values and must be initialized before use.
| Data Type | Default Value |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | '\u0000' (null character) |
| boolean | false |
| Objects (e.g., String) | null |
This table applies to both instance and class (static) variables. Local variables must be explicitly initialized, or the compiler will throw an error.
Understanding variable types and their scopes is crucial for effective Java programming.
Topic: Variables | Language: Java
public class Counter {
static int count = 0;
void increment() {
count++;
}
}public class Test {
int num = 10; // instance variable
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t1.num = 20;
System.out.println(t1.num + " " + t2.num);
}
}public class Test {
int x; // instance variable
static int y; // static variable
public static void main(String[] args) {
Test obj = new Test();
System.out.println(obj.x);
System.out.println(y);
}
}public class Example {
public static void main(String[] args) {
int a; // local variable
System.out.println(a);
}
}