Variables declared in methods and in blocks are called local variables. Local variable are not initialized when they are created at method invocation. Therefore, a local variable must be initialized explicitly before being used. Otherwise the compiler will flag it as error when the containing method or block is executed.
Example:
public class SomeClassName{
public static void main(String args[]){ int total; System.out.println("The incremented total is " + total + 3); //(1)
}}
The compiler complains that the local variable total used in println statement at (1) may not be initialized.
Initializing the local variable total before usage solves the problem:
public class SomeClassName{
public static void main(String args[]){int total = 45; //Local variable initialized with value 45 System.out.println("The incremented total is " + total+ 3); //(1)
}}
An instance variable is initialized with the default value of its type each time the class is instantiated, that is for every object created from the class.
A static variable is initialized with the default value of its type when the class is first loaded.
Note: Reference fields are always initialized with the value null if no initialization is provided
Example of default values for fields
public class House{
// Static variable
static long similarHouses; //Default value 0L when class is loaded.
// Instance variables
String houseName; //Implicitly set to default value null
int numberOfRooms=5; // Explicitly set to 5
boolean hasPet; // Implicitly set to default value false
//..
}
Example:
public class SomeClassName{
public static void main(String args[]){ int total; System.out.println("The incremented total is " + total + 3); //(1)
}}
The compiler complains that the local variable total used in println statement at (1) may not be initialized.
Initializing the local variable total before usage solves the problem:
public class SomeClassName{
public static void main(String args[]){int total = 45; //Local variable initialized with value 45 System.out.println("The incremented total is " + total+ 3); //(1)
}}
Fields initialization
If no initialization is provided for an instance or static variable, either when declared or in an initializer block, then it is implicitly initialized with the default value of its type.An instance variable is initialized with the default value of its type each time the class is instantiated, that is for every object created from the class.
A static variable is initialized with the default value of its type when the class is first loaded.
Data Type | Default Value |
boolean | false |
char | '/u0000' |
Integer(byte,short,int,long) | 0L for long, 0 for others |
Floating-point(float,double) | 0.0F or 0.0D |
reference type | null |
Note: Reference fields are always initialized with the value null if no initialization is provided
Example of default values for fields
public class House{
// Static variable
static long similarHouses; //Default value 0L when class is loaded.
// Instance variables
String houseName; //Implicitly set to default value null
int numberOfRooms=5; // Explicitly set to 5
boolean hasPet; // Implicitly set to default value false
//..
}
No comments:
Post a Comment