A variable in Java can be thought of as a box that stores data values during the execution of a Java program. The box has a fixed size, defined by the type of the variable, but its contents can be changed. Variables are the basic unit of storage in a Java program, and each variable has a name representing its memory location.
- Local Variable
- Instance Variable
- Class/Static Variable
Before using a variable, you must declare it by specifying its data type and name.
Example:
int age; // Declaration of an integer variable named age
After declaring a variable, you can optionally initialize it by assigning it an initial value. Uninitialized variables have default values depending on their type.
Example:
int age = 25; // Initialization of the age variable with a value of 25
Java supports various data types for variables, including:
- Primitive types (e.g.,
int
,double
,boolean
,char
) - Reference types (e.g., objects and arrays)
- Declared inside methods, constructors, or blocks.
- Created when the method, constructor, or block is entered and destroyed when it exits.
- Cannot have access modifiers (e.g.,
private
,public
). - Must be initialized before the first use as they do not have default values.
public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
System.out.println("Puppy age is: " + age);
}
public static void main(String[] args) {
Test test = new Test();
test.pupAge();
}
}
Output:
Puppy age is: 7
public class Test {
public void pupAge() {
int age;
age = age + 7;
System.out.println("Puppy age is: " + age);
}
public static void main(String[] args) {
Test test = new Test();
test.pupAge();
}
}
Output:
Error: variable 'age' might not have been initialized
- Declared in a class but outside methods, constructors, or blocks.
- Created when an object is instantiated and destroyed when the object is destroyed.
- Typically made private but can have other access modifiers.
- Have default values:
0
for numeric types,false
forboolean
, andnull
for object references.
public class Employee {
public String name; // Instance variable
private double salary; // Instance variable
public Employee(String empName) {
name = empName;
}
public void setSalary(double empSal) {
salary = empSal;
}
public void printEmp() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
public static void main(String[] args) {
Employee empOne = new Employee("Ransika");
empOne.setSalary(1000);
empOne.printEmp();
}
}
Output:
Name: Ransika
Salary: 1000.0
- Declared with the
static
keyword in a class but outside methods, constructors, or blocks. - Shared among all objects of the class.
- Typically used for constants or class-wide properties.
- Have default values similar to instance variables.
public class Employee {
private static double salary; // Static variable
public static final String DEPARTMENT = "Development"; // Constant
public static void main(String[] args) {
salary = 1000;
System.out.println(DEPARTMENT + " average salary: " + salary);
}
}
Output:
Development average salary: 1000
Feature | Local Variables | Static Variables | Instance Variables |
---|---|---|---|
Definition | Variables declared inside a method, constructor, | ||
or block. | Variables declared with the static keyword, shared among all instances of a class. |
Variables declared in a class but outside any method, specific to each instance. | |
Scope | Limited to the method, constructor, or block. | Throughout the class, accessible via class or instance. | Limited to the instance of the class. |
Lifetime | Exists only during the method/block execution. | Exists for the lifetime of the class. | Exists as long as the object exists. |
Default Value | None; must be explicitly initialized. | 0 (numeric), false (boolean), null (object references). |
0 (numeric), false (boolean), null (object references). |
Memory Allocation | Stack memory. | Static memory of the heap. | Heap memory (as part of the object). |
Access Modifiers | Cannot have access modifiers. | Can have access modifiers. | Can have access modifiers. |
Usage | Temporary storage/calculations in methods. | Class-wide fields, constants, utility methods. | Attributes unique to each object. |
Example Declaration | int localVar = 10; |
static int staticVar = 10; |
int instanceVar = 10; |
Access | Directly within the method. | ClassName.staticVar or objectName.staticVar |
objectName.instanceVar |
Q1. What are Java variables?
Java variables are containers that hold data values. They are used to store and manipulate data within a Java program.
Example:
int age = 25; // 'age' is a variable holding the value 25
Q2. How do you declare a variable in Java?
Variables in Java are declared by specifying the data type followed by the variable name. For Example:
int age; // Declaration
Example:
int age; // Declaration of the variable 'age'
age = 25; // Assignment
Q3. What are the different types of variables in Java?
Java variables can be classified into three main types:
- Local variables: Declared within a method, constructor, or block.
- Instance variables: Associated with objects (instances) of a class.
- Static variables: Associated with the class itself.
Example:
public class Example {
int instanceVar; // Instance variable
static int staticVar; // Static variable
public void method() {
int localVar = 5; // Local variable
}
}
Q4. What is the scope of a variable in Java?
In Java, the scope of a variable determines where programmers can access it in the code. Local variables confine their scope within the block of code where developers declare them, whereas instance and static variables offer broader scopes.
Example:
public class Example {
int instanceVar; // Accessible throughout the class
static int staticVar; // Accessible throughout the class
public void method() {
int localVar = 5; // Only accessible within 'method'
System.out.println(localVar); // OK
}
}
Q5. Can variable names start with a digit in Java?
No, variable names in Java cannot start with a digit. They must begin with a letter (A-Z, a-z), dollar sign ($), or underscore (_). After the first character, variable names can contain digits (0-9).
Example:
int validVar = 5; // Valid
int 1invalidVar = 10; // Invalid (starts with a digit)
Q6. Can Java variables be redeclared within the same scope?
No, Java does not allow redeclaration of variables within the same scope. Each variable name must be unique within its scope.
Example:
int age = 30;
int age = 35; // Error: 'age' is already declared
Q7. What is the difference between instance and static variables?
Instance variables associate with objects (instances) of a class, creating separate copies for each instance. On the other hand, static variables associate with the class itself, maintaining only one shared copy among all instances of the class.
Example:
class Example {
int instanceVar; // Instance variable
static int staticVar; // Static variable
public Example(int value) {
instanceVar = value;
}
}
Q8. Can final variables be modified in Java?
In Java, you cannot modify final variables once you initialize them. They function as constants, and their values remain unchanged after initialization.
Example:
final int maxLimit = 100;
maxLimit = 200; // Error: Cannot assign a value to final variable 'maxLimit'
Q9. What are the naming conventions for Java variables?
Variable names in Java should follow certain naming conventions for readability and consistency. It is recommended to use meaningful names that describe the purpose of the variable, with the first letter lowercase and subsequent words capitalized (camelCase).
Example:
int totalAmount = 50; // Camel case for variable name
Q10. Do Java variables have default values?
Yes, Java variables have default values if not explicitly initialized. The default value depends on the variable’s data type. For example, numeric types have a default value of 0, false for boolean, and null for reference types.
Example:
int number; // Default value is 0
boolean isValid; // Default value is false
String name; // Default value is null
Q11. What is the difference between instance variable and class variable?
Instance variables hold unique data for each object, while class variables share a single value across all objects.
Example:
class Example {
int instanceVar; // Instance variable
static int classVar; // Class variable
public Example(int value) {
instanceVar = value;
}
}
Q12. Where are instance variables stored in Java?
Instance variables in Java are stored in the heap memory, which is allocated to each individual object created from a class. Each object has its own copy of instance variables.
Example:
class Example {
int instanceVar; // Stored in heap memory when the object is created
}
Example obj = new Example();
obj.instanceVar = 5; // Each object has its own instanceVar
Q13. Can instance variables be declared as static?
No, instance variables cannot be declared as static. Static variables belong to the class, not to specific instances. However, instance variables are specific to each object, while static variables are shared across all instances of the class.
Example:
class Example {
static int staticVar; // Static variable
int instanceVar; // Instance variable
}
Q14. When are instance variables created and destroyed?
Instance variables are created when an object is instantiated (in the constructor). They are destroyed when the object is eligible for garbage collection, i.e., when there are no more references to the object.
Example:
class Example {
int instanceVar; // Instance variable created when object is created
public Example() {
instanceVar = 10; // Initialization
}
}
Example obj = new Example(); // instanceVar is created
obj = null; // instanceVar is destroyed when obj is garbage collected
PrabhatDevLab, PrabhatKumar.com, LinkedIn, Stackoverflow, GitHub, LeetCode, HackerRank, GeeksforGeeks, AlgoByPrabhat, SHARMA AI, About Me, Instagram, Twitter
Happy Learning! 📚✨ Keep exploring and growing your knowledge! 🚀😊