static-initializer

Why doesn't Java allow to throw a checked exception from static initialization block?

孤街醉人 提交于 2020-04-23 05:50:53
问题 Why doesn't Java allow to throw a checked exception from a static initialization block? What was the reason behind this design decision? 回答1: Because it is not possible to handle these checked exceptions in your source. You do not have any control over the initialization process and static{} blocks cannot be called from your source so that you could surround them with try-catch. Because you cannot handle any error indicated by a checked exception, it was decided to disallow throwing of

Why doesn't Java allow to throw a checked exception from static initialization block?

你。 提交于 2020-04-23 05:46:55
问题 Why doesn't Java allow to throw a checked exception from a static initialization block? What was the reason behind this design decision? 回答1: Because it is not possible to handle these checked exceptions in your source. You do not have any control over the initialization process and static{} blocks cannot be called from your source so that you could surround them with try-catch. Because you cannot handle any error indicated by a checked exception, it was decided to disallow throwing of

java static initializer called twice

☆樱花仙子☆ 提交于 2019-12-25 05:46:09
问题 static boolean isClassLoaded(String fullname) { try { Class.forName(fullname, false, Loader.instance().getModClassLoader()); return true; } catch (Exception e) { return false; } } does this method has potential to trigger fullname's static initializer ? i have problem with static initializer called twice. when i try to check if class loaded using isClassLoaded and try to use that class, i get error because of constructor called twice. anyone know what is problem with Class.forName(fullname,

Using a macro to initialize a big array of non-Copy elements

家住魔仙堡 提交于 2019-12-20 02:47:34
问题 I'm trying to initialize a big array of elements with the same initializer. 64 elements is just an example — I want to make it at least 16k. Unfortunately a simple let array : [AllocatedMemory<u8>; 64] = [AllocatedMemory::<u8>{mem:&mut []};64]; won't work because the AllocatedMemory struct does not implement Copy error: the trait `core::marker::Copy` is not implemented for the type `AllocatedMemory<'_, u8>` [E0277] let array : [AllocatedMemory<u8>; 64] = [AllocatedMemory::<u8>{mem:&mut []};

A better way to initialize a static array member of a class in C++ ( const would be preferred though )

点点圈 提交于 2019-12-12 01:59:06
问题 I have a static array of pointers to functions as a member of a class. I need to initialize it, but it turns out this array is 64K items long, so it's impractical to initialize it with a static initializer like { x, y, z, ... } as it would clutter code. I have instead to initialize it by code, with several loops. The way I figured to do this is by initializing the static array in the constructor and setting a flag to it, so only the construction of the first instance of the class would fire

Static initializer cannot reference a field before it is defined

时光总嘲笑我的痴心妄想 提交于 2019-12-10 10:58:33
问题 I have the following code with the error commented public final class MyStaticClass { private MyStaticClass() {} static { a = new A(); b = new B(a); // Cannot access a field before it is defined } private static final A a; private static final B b; } I'm fairly new to using static initializers, but I have no idea why this will not compile. I've looked around a few of the posts on this topic, and have seen the order that initialisation runs, but this doesn't seem to violate the rule. By the

C++0x static initializations and thread safety

最后都变了- 提交于 2019-12-10 02:02:16
问题 I know that as of the C++03 standard, function-scope static initializations are not guaranteed to be thread safe: void moo() { static std::string cat("argent"); // not thread safe ... } With the C++0x standard finally providing standard thread support, are function-scope static initializations required to be thread safe? 回答1: it seems the initialization would be thread safe, since in the case the object is dynamically initialized upon entering the function, it's guaranteed to be executed in a

How to check whether a class is initialized?

大兔子大兔子 提交于 2019-12-10 01:48:04
问题 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? 回答1: 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

Static initializer cannot reference a field before it is defined

喜欢而已 提交于 2019-12-06 04:35:23
I have the following code with the error commented public final class MyStaticClass { private MyStaticClass() {} static { a = new A(); b = new B(a); // Cannot access a field before it is defined } private static final A a; private static final B b; } I'm fairly new to using static initializers, but I have no idea why this will not compile. I've looked around a few of the posts on this topic, and have seen the order that initialisation runs, but this doesn't seem to violate the rule. By the time b is being initialized, a should already have been. I have a work around, which would be to set up

C++0x static initializations and thread safety

邮差的信 提交于 2019-12-05 02:35:42
I know that as of the C++03 standard, function-scope static initializations are not guaranteed to be thread safe: void moo() { static std::string cat("argent"); // not thread safe ... } With the C++0x standard finally providing standard thread support, are function-scope static initializations required to be thread safe? it seems the initialization would be thread safe, since in the case the object is dynamically initialized upon entering the function, it's guaranteed to be executed in a critical section: § 6.7 stmt.decl 4. ...such an object is initialized the first time control passes through