java.lang.NullPointerException (aka NPE). Yep! You're using nothing thinking it is something, roughly speaking.
A simple example:
Here is your car:
public class MyCar{
public void startEngin(){
}
Here are some cases where java.lang.NullPointerException will show up:
class Driver(){
MyCar myCar;
public void drive(){
//trying to start the engine of a car not yet created.
//we should've created it like this: myCar = new MyCar();
myCar.startEngine();
}
}
You're using an object referencing null.
Java Tutorials
Class members modifier: final
The keyword final is a modifier that can prefix both class fields and methods. If a variable is prefixed with the modifier final, then it is in reality a constant that can't be modified once initialized with a value. This applies to instance, static and local variables, including parameters that are declared final.
Note: final variable need not be initialized at its declaration, but it must be initialized once before being used.
A final method in a class is complete (i.e., has an implementation) and can't be overridden in any subclass. That is, a subclass can't change its behavior.
So final variables ensure that values can't be changed and final methods ensure that behavior can't be changed.
Note: Variables declared in an interface are implicitly final.
- A final variable of a primitive data type can't change its value once it has been initialized.
- A final variable of a reference type can't change its reference value once it has been initialized, but the state of the object it denotes can still be changed.
Note: final variable need not be initialized at its declaration, but it must be initialized once before being used.
A final method in a class is complete (i.e., has an implementation) and can't be overridden in any subclass. That is, a subclass can't change its behavior.
So final variables ensure that values can't be changed and final methods ensure that behavior can't be changed.
Note: Variables declared in an interface are implicitly final.
Class members modifier: static
Members (fields and methods) of a class may be prefixed by the keyword static to distinguish them from instance (i.e.,non-static) members.
Static members belong to the class they are declared in but make no part of any instance of that class.
When the class is loaded, static variables (also called class variables) are initialized either explicitly by an initialization expression or given default values, these values are not part of the state of any object. In other words, static variables are not instantiated if an instance of the class is created.
There is no notion of object associated with static methods (also known as class methods), therefore a static method in a class can't access instance (i.e.,non-static) members. A static method can access directly other static members in the class. However static methods can always use a reference of the class' type to access its static and instance members.
Clients may access static members through the object reference of the class or simply by using the class name. Anyway, the class need not be instantiated to access its static members.
Static members belong to the class they are declared in but make no part of any instance of that class.
When the class is loaded, static variables (also called class variables) are initialized either explicitly by an initialization expression or given default values, these values are not part of the state of any object. In other words, static variables are not instantiated if an instance of the class is created.
There is no notion of object associated with static methods (also known as class methods), therefore a static method in a class can't access instance (i.e.,non-static) members. A static method can access directly other static members in the class. However static methods can always use a reference of the class' type to access its static and instance members.
Clients may access static members through the object reference of the class or simply by using the class name. Anyway, the class need not be instantiated to access its static members.
What's the lifetime of variables?
During the execution of a Java program, each variable has its own time within which it can be accessed. This is called the lifetime of the variable. We can determine the lifetime of variables by looking at the context in which they're defined. Anyway, we can distinguish between three contexts:
Instance variables:
Instance variables are class members. Every time you create an object from a class, a brand new copy of these instance variables is created for this object. Actually, creating an object from a class means, partially, creating copies of instance variables for that object. So each object has its own copy of instance variables which exist as long as the object they belong to exists. The values of these variables constitute what we call: the state of the object.
Static variables:
Static variables are also members of a class but can't belong to any object created from that class. So created objects from the class don't get their own copies of static variables. Therefore static variables are created only when the class is loaded at runtime. The existence of static variables is dependent only on the class itself. As a result, a static variable exists as long as its class exists.
Please note that an object of a class may still manipulate a static variable even though this object can't have its own copy of this static variable.
Local variables:
Also called automatic variables, are called local because their scope is the method or block within which they are declared. After the execution of the method or block completes, these local (non-final) variables are no longer accessible.
Instance variables:
Instance variables are class members. Every time you create an object from a class, a brand new copy of these instance variables is created for this object. Actually, creating an object from a class means, partially, creating copies of instance variables for that object. So each object has its own copy of instance variables which exist as long as the object they belong to exists. The values of these variables constitute what we call: the state of the object.
Static variables:
Static variables are also members of a class but can't belong to any object created from that class. So created objects from the class don't get their own copies of static variables. Therefore static variables are created only when the class is loaded at runtime. The existence of static variables is dependent only on the class itself. As a result, a static variable exists as long as its class exists.
Please note that an object of a class may still manipulate a static variable even though this object can't have its own copy of this static variable.
Local variables:
Also called automatic variables, are called local because their scope is the method or block within which they are declared. After the execution of the method or block completes, these local (non-final) variables are no longer accessible.
What are variables default values?
Local variables Initialization
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)
}
}
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
//..
}
Subscribe to:
Posts (Atom)