static-block

How to initialize ThreadLocal objects in Java

橙三吉。 提交于 2019-11-26 23:14:27
问题 I'm having an issue where I'm creating a ThreadLocal and initializing it with new ThreadLocal . The problem is, I really conceptually just want a persistent list that lasts the life of the thread, but I don't know if there's a way to initialize something per-thread in Java. E.g. what I want is something like: ThreadLocal static { myThreadLocalVariable.set(new ArrayList<Whatever>()); } So that it initializes it for every thread. I know I can do this: private static Whatever getMyVariable() {

What's the C++ idiom equivalent to the Java static block?

岁酱吖の 提交于 2019-11-26 17:47:59
I have a class with some static members, and I want to run some code to initialize them (suppose this code cannot be converted into a simple expression). In Java, I would just do class MyClass { static int myDatum; static { /* do some computation which sets myDatum */ } } Unless I'm mistaken, C++ does not allow for such static code blocks, right? What should I be doing instead? I would like solution for both of the following options: Initialization happens when process loads (or when the DLL with this class is loaded). Initialization happens when the class is first instantiated. For the second

Behavior of static blocks with inheritance

有些话、适合烂在心里 提交于 2019-11-26 16:05:42
问题 I am trying to use static blocks like this: I have a base class called Base.java public class Base { static public int myVar; } And a derived class Derived.java : public class Derived extends Base { static { Base.myVar = 10; } } My main function is like this: public static void main(String[] args) { System.out.println(Derived.myVar); System.out.println(Base.myVar); } This prints the out put as 0 0 where as I expected 10 0 . Can somebody explain this behavior? Also, if I want my derived

In what order do static blocks and initialization blocks execute when using inheritance?

我的梦境 提交于 2019-11-26 15:42:53
I have two classes Parent and Child public class Parent { public Parent() { System.out.println("Parent Constructor"); } static { System.out.println("Parent static block"); } { System.out.println("Parent initialisation block"); } } public class Child extends Parent { { System.out.println("Child initialisation block"); } static { System.out.println("Child static block"); } public Child() { System.out.println("Child Constructor"); } public static void main(String[] args) { new Child(); } } The output of the above code will be Parent static block Child static block Parent initialization block

Static block vs. initializer block in Java? [duplicate]

人走茶凉 提交于 2019-11-26 08:48:22
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Static Initialization Blocks Consider the following code : public class Test { { System.out.println(\"Empty block\"); } static { System.out.println(\"Static block\"); } public static void main(String[] args) { Test t = new Test(); } } We understand that first the static block would be executed followed by the empty block. But the problem is that I have never been able to understand the real utility of an empty

In what order do static blocks and initialization blocks execute when using inheritance?

有些话、适合烂在心里 提交于 2019-11-26 04:34:54
问题 I have two classes Parent and Child public class Parent { public Parent() { System.out.println(\"Parent Constructor\"); } static { System.out.println(\"Parent static block\"); } { System.out.println(\"Parent initialisation block\"); } } public class Child extends Parent { { System.out.println(\"Child initialisation block\"); } static { System.out.println(\"Child static block\"); } public Child() { System.out.println(\"Child Constructor\"); } public static void main(String[] args) { new Child(

Static Initialization Blocks

♀尐吖头ヾ 提交于 2019-11-25 22:27:17
问题 As far as I understood the \"static initialization block\" is used to set values of static field if it cannot be done in one line. But I do not understand why we need a special block for that. For example we declare a field as static (without a value assignment). And then write several lines of the code which generate and assign a value to the above declared static field. Why do we need this lines in a special block like: static {...} ? 回答1: The non-static block: { // Do Something... } Gets