In Java programming, the initialization of variables holds significant importance as it defines the initial values assigned to these variables. In this blog we will look into intializing local, instance and class variables.
Table of Contents
1. Local variables
Local variables are declared and initialized within methods, constructors or initializer blocks. Local variables do not have a default value and must be explicitly initialized before use.
public class VariableInitializationExample {
public void exampleMethod() {
int localVar = 10; // Local variable is initalized here
System.out.println(localVar); // localVar will be printed
int localVar2; // localVar2 is declared, but not inilialized
System.out.println(localVar2); /* Compilation error: Variable 'localVar' might not have been initialized */
}
}
2. Constructor and Method Parameters
Constructor and method parameters are variables that are passed to a constructor or method. These parameters are local variables that have been initialized before the method is called.
public class ParameterInitializationExample {
public static void exampleMethod(boolean param) {
System.out.println(param);
}
public static void main(String[] args) {
boolean example = true;
exampleMethod(example); // will print true
exampleMethod(); /* Compilation error: The local variable 'param' may not have been initialized */
}
}
3. Instance and Class variables
In Java, member fields play a crucial role in defining the state of objects within a class. Two main types of member fields are instance variables and class variables. Let’s explore the concepts of instance and class variables, also known as member fields, and understand their characteristics.
3.1. Instance Variables
Instance variables are fields that belong to a specific instance of an object. Each instance of a class has its own copy of instance variables, allowing objects to have unique states.
public class MyClass {
// Instance variables
int instanceVar1;
String instanceVar2;
}
In the example above, instanceVar1
and instanceVar2
are instance variables. Each object created from MyClass
will have its own set of these variables.
3.2. Class Variables (Static variables)
Class variables, or static variables, are defined at the class level and shared among all instances of the class. They are declared with the static
keyword. Unlike instance variables, class variables are associated with the class itself, not individual objects.
public class MyClass {
// Class variable (static)
static int classVar;
}
In this case, classVar
is a class variable. All instances of MyClass
share the same classVar
.
3.3. Default Values for Class Variables
When class variables are not explicitly initialized, Java assigns default values based on their types. Here’s a table depicting the default values for common types:
Type | Default Value |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | ‘\u0000’ |
boolean | false |
Object | null |
It’s important to note that these default values are assigned when the class is loaded into memory, and they are shared among all instances of the class.
example:
public class Car {
// Instance variables
String model;
int year;
// Class variable (static)
static int totalCars;
// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
totalCars++;
}
public static void main(String[] args) {
System.out.println("totalCars: " + Car.totalCars);
System.out.print(Car.model); /* Does not compile, because model is instance variable */
Car car = new Car("BMW", 2024);
System.out.println("model: " + car.model);
System.out.println("totalCars: " + Car.totalCars);
}
}
output:
totalCars: 0
model: BMW
totalCars: 1