89 questions
2
votes
1
answer
36
views
Between outer and inner classes, their static initializers run order is different when invoking main method inside or outside the outer class
I have the following code:
class Foo {
static { //static initializer block
System.out.print("Foo");
}
class Bar {
static { //static initializer block
...
0
votes
1
answer
61
views
Run some static setup for all tests in a module in JUnit
I have multimodule gradle project each of which contains a number of tests. For each module before running all tests I'd like to perform static fields and other configuration initialization so the ...
2
votes
1
answer
184
views
Inherit execution of static initializer blocks in JavaScript
I have code like:
class A {
static {
console.log("A");
}
}
class B extends A {
static {
console.log("B");
}
}
Why doesn't it print A twice? I would ...
0
votes
1
answer
222
views
Java static factory to create not thread safe object
In the Clean Code book there is an example which I would like to understand better:
public static SimpleDateFormat makeStandardHttpDateFormat() {
// SimpleDateFormat is not thread safe, so we need ...
-2
votes
2
answers
428
views
Java static initializers
I'm learning java by working through some hacker rank problems. The below code is about learning about static initializer block. The exception is thown and caught but the program keeps running and I ...
1
vote
1
answer
118
views
Static initializing block is not working as expected
I have two classes, "Test1" and "Test0", as shown in this code.
public class Test1 {
public static void main(String...args) {
System.out.print(Test0.randomName);
}
}...
1
vote
0
answers
1k
views
Run Initialization Code when a Script is Loaded
I have a GDScript file, and I would like to be able to run a block of code whenever the script is loaded. I know _init will run whenever an instance is constructed and _ready will run when it's added ...
1
vote
3
answers
430
views
Default Constructor is getting executed before Static block in java
When we load a class in java, first static block get executed and then default constructor. but in below peace of code, What I observed that default constructor is getting executed before static block....
1
vote
0
answers
46
views
Detailed Initialization Procedure class variable initializer
I saw many confusing answers about the following point :
Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in ...
0
votes
1
answer
583
views
Is it appropriate to catch java.lang.ExceptionInInitializerError in multi-threaded application?
I have a class that loads and parse data from XML files in a static initializer like so:
class A {
//static structures to store parsed XML data
public static int num;
...
static {
...
0
votes
1
answer
2k
views
How to Stop static initialization with PowerMockito
I am working on an API for work, we use a shared library for multiple projects for the purposing of our logging framework. The class used uses all static methods for its calls.
I am trying to Unit ...
-1
votes
3
answers
113
views
Can assign a static variable but cannot print it out in a static initializer [duplicate]
I am able to assign value to a static variable but am not able to print it out in the same static block.
If I move the static variable above the static block, then all works well. Now I am failing to ...
4
votes
3
answers
2k
views
Unable to read input from static block using scanner class
i want to read the data from the user in the static block and need to check some condition there but when i am trying to call nextInt() it causes some error
public class Test {
static int B,H;
...
0
votes
1
answer
511
views
Is it a good practice to use static initializers?
Is there any alternative to static initializers in Java?
Just a random example:
private static List<String> list;
static {
list = new ArrayList<>();
list.add("foo")
}
Doesn't ...
6
votes
4
answers
4k
views
can not initialize static final variable in try/catch
I am trying to initialize a static final variable. However, this variable is initialized in a method which can throw exception, therefor, I need to have inside a try catch block.
Even if I know that ...