Difference between static block and assigning static in class?
java, static
Solution
Yes, its functionally the same.
From Java doc
There is an alternative to static blocks — you can write a private static method:
class Whatever {
public static varType myVar = initializeClassVariable();
private static varType initializeClassVariable() {
// initialization code goes here
}
}
The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.
Problem
Is there any difference between following two initializations of static variables: ``` class Class1 { private static Var var; static { var = getSingletonVar(); } } ``` ``` class Class2 { private static var = getSingletonVar; } ``` Are these two different ways of initializing a static variable functionally the same?