Static Initialization Blocks
by , 04-26-2012 at 04:53 PM (452 Views)
A static initialization block is considered to be a normal code block which is enclosed in braces, { }. It is however preceded, by the static keyword. For example:
A class may consist of any static initialization blocks number.They might be present in the class body, anywhere. It is guaranteed by the runtime system to call static initialization blocks in similar order in which they are preent in source code.Java Code:static { // whatever code is needed for initialization goes here }
Alternatively, one might write the private static method:
class Whatever {
public static varType myVar = initializeClassVariable();
private static varType initializeClassVariable() {
// initialization code goes here
}
}
[/code]
The private static methods advantage is that one might use them later on if reinitialization of the class variable is required.









Email Blog Entry
Size Reduced for Images in PDF &...
05-15-2013, 05:53 PM in Java Software