Questions tagged [static-initializer]

The static initializer is a static {} block of code inside java class, and run only one time before the constructor or main method is called.

89 questions
10
votes
1 answer

C++0x static initializations and thread safety

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…
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
9
votes
4 answers

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

I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{ QuestionFactory.registerType("TrueFalse", "Question"); } public TrueFalseQuestion(){} } ... public class QuestionFactory { static…
randomThought
  • 6,203
  • 15
  • 56
  • 72
9
votes
7 answers

Use of static init block

I know how a static init block works. Can anyone please tell me some typical uses of it.
gameover
  • 11,813
  • 16
  • 59
  • 70
8
votes
6 answers

Invoke static initializer again

Once a class is loaded is there a way to invoke static initializers again? public class Foo { static { System.out.println("bar"); } } Edit: I need to invoke the static initializer because I didn't write the original class and the…
Kalecser
  • 1,143
  • 12
  • 15
8
votes
2 answers

Loading, Linking, and Initializing - When does a class get loaded?

My understanding of classloading was that a class gets loaded when it is first needed (to put it in a very simple way). Running the following sample with -verbose:class and a modified version of the Iterators class that prints a message when its…
Haasip Satang
  • 457
  • 3
  • 17
8
votes
3 answers

C code involving {}

I saw this in some C code: Wininfo W = { sizeof(Wininfo) }; What the heck does this mean?
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
7
votes
1 answer

C# Static Initializer With (and Without) Mixed Static Constructors

I've been through the relevant section of C# Language Spec (v5.0) but I can't find the piece that's relevant to what I'm seeing. If you have a run of the code below, you'll see the output below, which is what I expect: using System; class Test { …
Tom Baxter
  • 2,110
  • 2
  • 19
  • 38
7
votes
3 answers

Why can't an inner class use static initializer?

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 …
Pacerier
  • 86,231
  • 106
  • 366
  • 634
6
votes
4 answers

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

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…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
6
votes
6 answers

Java basics: a static function without a name, or return type

public class Main { public static final Logger LOGGER = Logger.getLogger(Main.class.getName()); static { try { LOGGER.addHandler(new FileHandler("errors.log",true)); } catch(IOException ex) { …
Itako
  • 2,049
  • 3
  • 16
  • 19
6
votes
4 answers

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 variable will be either initialized on try or on…
Mayday
  • 4,680
  • 5
  • 24
  • 58
6
votes
3 answers

What does the {{ syntax on ArrayList initializer really do

I have recently found what appears to me to be a new syntax for statically initializing an ArrayList: new ArrayList() {{ add("first"); add("second"); }}; My question is, what is really happening there? Is that a shortcut for defining…
Gus
  • 3,534
  • 1
  • 30
  • 39
6
votes
3 answers

Can a Java static initializer call a static method?

Can I call a static method from a static initializer in Java? Is the following valid and guaranteed to work as per the Java specification? public class Foo { private final static int bar; private static int generateValue() { return 123; …
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
6
votes
2 answers

Static final field initialization from static initializer

Why isn't it possible to access static final fields from a corresponding static initializer using the declaring class as qualifier (in a static way)? At first, I thought this was an Eclipse bug: I also consisted some lack of knowledge, because…
Franz Ebner
  • 4,951
  • 3
  • 39
  • 57
6
votes
6 answers

Java: what is static{}?

Can someone explain me what the following is? public class Stuff { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch ( ClassNotFoundException exception ) { …
Omu
  • 69,856
  • 92
  • 277
  • 407