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
1
vote
0 answers

static initializer interfering with emma

I've written a class that has every line covered through JUnit/emma (its a fairly basic class). Emma's eclipse output shows all lines in green (covered). However, emma's report (HTML) shows that I've missed 1 instruction. It highlights the class in…
panther ts
  • 21
  • 2
1
vote
4 answers

Static initializers and thread synchronization (.NET)

Static initializers are supposed to be executed once before the first reference to the class. It means that every time a class is accessed, a check should be performed whether the static initializers for the class are executed. It seems that in…
mfeingold
  • 7,094
  • 4
  • 37
  • 43
0
votes
2 answers

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

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…
Petruza
  • 11,744
  • 25
  • 84
  • 136
0
votes
1 answer

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 tests will be run using this set up. Here is what I…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
0 answers

Java: documenting a static initializer yields a InvalidJavadocPosition in checkstyle

I endowed a static initializer with a javadoc comment. Now I face that checkstyle complains considering it an InvalidJavadocPosition. Is this a bug or correct? If the latter how to document a static initializer??
user2609605
  • 419
  • 2
  • 14
0
votes
1 answer

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 to create each instance independently …
evolved
  • 1,850
  • 19
  • 40
0
votes
7 answers

Avoiding new() in initialization of static members?

The code in question is as follows: header: class Vec3d : public Object { public: static linearalgebra::Vec3d* X_AXIS; static linearalgebra::Vec3d* Y_AXIS; static linearalgebra::Vec3d* Z_AXIS; static…
arataj
  • 373
  • 3
  • 12
0
votes
2 answers

populating map globally

I have declared the following map globally and trying to populate globally. 1: typedef std::map> DeviceTypeList; 2: DeviceTypeList g_DeviceTypeList; 3: g_DeviceTypeList.insert( std::make_pair…
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133
0
votes
2 answers

How to do a static initialization in Cocoa

When you need to initialize a static variable in Java, you can do something like that: public class MyClass { private static Object someStaticObject; static { // initialize someStaticObject here } ... How can you do the same in…
double07
  • 2,565
  • 2
  • 22
  • 22
0
votes
1 answer

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 { try { //load/parse XML data …
user3303411
  • 57
  • 1
  • 7
0
votes
1 answer

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 test an API call, I can not have it call anything on…
0
votes
1 answer

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 list; static { list = new ArrayList<>(); list.add("foo") } Doesn't it make debugging harder?
Mahozad
  • 18,032
  • 13
  • 118
  • 133
0
votes
2 answers

Weird singleton initialization in static function initialize during unit tests

I have a following code in my singleton class static MySingleton *gManager; +(void)initialize { if(self == [MySingleton class]) { gManager = [[MySingleton alloc] initWithServices:[[MyServices alloc] init]]; } } +(MySingleton…
0
votes
1 answer

Can a thread enter a static method before static initialization (class loading) is complete by another thread?

Lets say we have the following classes and two threads t1,t2. public class A { static String str = "abc"; static { B.bMeth(); } static void aMeth() { System.out.println("A::meth()"); …
0
votes
1 answer

Initialization in Annotation Types and static block

I've been trying to do some logic upon class loading of an annotation type. Naturally the static block came to my mind. But unfortunately this isn't allowed, the compile error in Eclipse is: Syntax error, insert "enum Identifier" to complete…
Mordechai
  • 15,437
  • 2
  • 41
  • 82