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.

  • 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.

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.

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
booleanfalse
char
'/u0000'
Integer(byte,short,int,long)0L for long, 0 for others
Floating-point(float,double)0.0F or 0.0D
reference typenull


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

//..
}


How to declare and initialize a variable

A variable in Java has a type, name and a value. A variable may store of value of either:
- a primitive data type such as int, boolean, char.
or
- a reference to an object, also called reference variable.


for example:
int i; //variable i that can store an int value.
boolean isDone; //Variable isDone that can store a boolean value.
//Variables a, f and r that can store a char value each:
char a,f,r;

//Equivalent to the three separate declarations:
char a;
char f;
char r;

What we just deed above is called variable declaration. By declaring variable we are implicitly allocating memory for these variables and determining the value types that can be stored in them.
So, in the example above, we named a variable: isDone that can store a boolean value, but not initialized yet.

Giving a variable a value when declared is called initialization. Further, we can declare and initialize a variable in one go. For example, we could have declared and initialized our variables of the previous example:

int i = 101; //variable i initialized with the value 101:
//variable isDone initialized with the boolean value true.
boolean isDone = true;
// variables a,f and r initialized with the values 'a','f' and 'r' //respectively:
char a = 'a', f='f', r='r';
//
equivalent to:
char a = 'a';
char f = 'f';
char r = 'r';


Analogous to declaring variables to denote primitive values, we can declare a variable denoting an object reference, that's; a variable having one of the following reference types: a class, an array, or an interface.

For instance (see also Class Instantiation ):
//declaring a redCar and a blueCar of class Car.
Car redCar, blueCar;

Please note that declarations above do not create any object of type Car. These are just variables that can store references to objects of class Car but no reference is created yet.

A reference variable has to be instantiated before being used. So:
// declaring and initializing the redCar reference variable
Car redCar=new Car("red");
An object of class Car is created using the keyword new together with the Constructor call Car("red"), then stored in the variable redCar which is now ready to be manipulated.