private static Method
The static initialize block will be called on loading of the class, and will have no access to instance variables or methods.
Sample Program
package com.javalearning.basic;
public class StaticInitialize {
private static boolean a = getInitialize();
static
{
System.out.println("I am inside the static block");
}
public static void main(String[] args) {
if(a)
{
System.out.println("Your Libraries initizalized properly");
}
}
private static boolean getInitialize() {
int a =5,b=3;
System.out.println("Initialize libraries prior to static block and can be reused");
if(a>b)
return true;
else
return false;
}
}
Reason Behind
Static variable initialized prior to static block.
final static Method
private static boolean a = getInitialize();
private final static boolean getInitialize(){
// initialization code goes here
}
}
Advantage
Both can be reused later if you need to re-initialize the class variable. But calling non-final methods from final static method during instance initialization can cause problems.
No comments:
Post a Comment