static-initializer

How to check whether a class is initialized?

怎甘沉沦 提交于 2019-12-05 02:06:44
You'll probably ask, why would I want to do that - it's because I'm using a class (from an external library) which does stuff in its static initializer and I need to know whether it's been done or not. I looked at ClassLoader , but didn't find anything that looked useful. Any ideas? Colin Hebert You can use the ClassLoader.findLoadedClass() method. If it returns null, then the class isn't loaded. This way you don't load the class if it wasn't already loaded. WARNING : This code doesn't really work here, in the system ClassLoader, findLoadedClass() is protected, you need to override it with

get static initialization block to run in a java without loading the class

允我心安 提交于 2019-12-04 19:41:24
问题 I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{ QuestionFactory.registerType("TrueFalse", "Question"); } public TrueFalseQuestion(){} } ... public class QuestionFactory { static final HashMap<String, String > map = new HashMap<String,String>(); public static void registerType(String questionName, String ques ) { map.put(questionName, ques); } } public class FactoryTester { public static void main(String[] args) { System.out.println

.crt section? What does this warning mean?

北城以北 提交于 2019-12-04 19:23:21
问题 I've got this warning recently (VC++ 2010) warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators I'm assuming this is the Critical Section. It's been a while since my Operating Systems course, so I can't really figure out what this means. If I remember right, the Critical Section works with shared resources. So how is this warning related and what does it mean exactly? 回答1: No, CRT = C Run Time. It is support library that any program needs to get the

Do objects of built-in types have special static initialisation order precedence?

此生再无相见时 提交于 2019-12-04 03:22:56
问题 I'd have expected the following code to yield a segmentation fault (or otherwise UB): struct T { T(); }; T t; char const* str = "Test string"; T::T() { std::cout << str; // zero-initialised, only! } int main() {} That's because t is initialised before str . I'd expect str to hold the value (char const*)0 due to zero-initialisation. My interpretation of [C++11: 3.6.2/2] supports this. However, the above snippet appears to output the string as expected (and I confirmed the behaviour by also

get static initialization block to run in a java without loading the class

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 14:21:41
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{ QuestionFactory.registerType("TrueFalse", "Question"); } public TrueFalseQuestion(){} } ... public class QuestionFactory { static final HashMap<String, String > map = new HashMap<String,String>(); public static void registerType(String questionName, String ques ) { map.put(questionName, ques); } } public class FactoryTester { public static void main(String[] args) { System.out.println(QuestionFactory.map.size()); // This prints 0. I want it to print 1 } } How can I change TrueFalseQuestion class so

Request for the detailed description of “Static Initialization Order Fiasco”

僤鯓⒐⒋嵵緔 提交于 2019-12-02 14:34:23
问题 I read about the SIOF in the faq-lite and still I really don't understand why the issue happens. I have a static library(.a) and I use that library to use its static const data member object type. Then that static const data member object type I use and assign to a global variable(object). But it seems that global variable is empty when I use that global variable to my main or to any local function. I know obviously that my issue is SIOF but I really don't understand why my static const data

Request for the detailed description of “Static Initialization Order Fiasco”

做~自己de王妃 提交于 2019-12-02 04:31:13
I read about the SIOF in the faq-lite and still I really don't understand why the issue happens. I have a static library(.a) and I use that library to use its static const data member object type. Then that static const data member object type I use and assign to a global variable(object). But it seems that global variable is empty when I use that global variable to my main or to any local function. I know obviously that my issue is SIOF but I really don't understand why my static const data member object was not initialized. It was static library so I guess when we created our static library

Why can't an inner class use static initializer?

爷,独闯天下 提交于 2019-12-01 03:43:59
问题 Quoth JLS #8.1.3: Inner classes may not declare static initializers (§8.7)...... This is demonstrated as such: class A { class B { static { // Compile-time Error: Cannot define static initializer in inner type A.B System.out.println("Class is initializing..."); } } } Now since Java's inner (non-static) classes are loaded by class loaders just like every other class, why can't we have static initializers for them? What's the reason behind this limitation? 回答1: I think it is because the Inner

How do you disable lazy class loading/initialization in Sun's JVM?

こ雲淡風輕ζ 提交于 2019-11-27 14:44:28
By default, Sun's JVM both lazily loads classes and lazily initializes (i.e. calls their <clinit> methods) them. Consider the following class, ClinitBomb , which throws an Exception during a static{} block. public class ClinitBomb { static { explode(); } private static void explode() { throw new RuntimeException("boom!"); } } Now, consider how to trigger the bomb: public class Main { public static void main(String[] args) { System.out.println("A"); try { Class.forName("ClinitBomb"); } catch (Exception e) { e.printStackTrace(System.out); } System.out.println("B"); ClinitBomb o2 = new ClinitBomb

How to force a class to be initialised? [closed]

被刻印的时光 ゝ 提交于 2019-11-27 09:23:49
What is the best and cleanest way to do this? Specifically, I need some code in a static initializer block to run in that class, but I'd like to make this as clean-looking as possible. Loading != Initialization. You want your class to be initialized (this is when static blocks executed, among other things). An excerpt from the Java Language Specification says: A class or interface type T will be initialized immediately before the first occurrence of >any one of the following: T is a class and an instance of T is created. T is a class and a static method declared by T is invoked. A static field