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
0
votes
0 answers

Order of execution between static blocks and init functions

What is the defined execution order between static blocks and static initialization functions? Is it their order in the class definition? For example: public class Test { static {System.out.println("1"); } static int x = getX(); static…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
1 answer

Using constant objects in objective-c

I have a piece of code similar to this: //Foo.h OBJC_EXPORT MyObject *const myObj; // Foo.m MyObject *const myObj; @implementation Foo +(void) initialize { if (self = [Graph class]) { myObj = [Config get:@"Foo"]; // <--- ERROR!…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
0
votes
2 answers

How to catch ExceptionInInitializerError?

I want to install some kind of global handler to catch any ExceptionInInitializerError which could be thrown from any static block when some class is loading. Currently it dumps a stack trace to the stderr and exits the application. I want to log a…
vbezhenar
  • 11,148
  • 9
  • 49
  • 63
0
votes
2 answers

How is it possible to create Local class in Java static block?

From Java documentation: Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method. Also, Local class is a…
Taisa
  • 26
  • 5
0
votes
0 answers

Class name in Java static initializer

In the code public class Test { public static final int var1; public static int var2; static { Test.var2 = 3; Test.var1 = Test.var2; } } javac says Test.java:8: error: cannot assign a value to final variable…
0
votes
1 answer

Overriding a NSMutableUrlRequest static initializer?

Heres my scenario. Most of my network calls now need to have an api key inserted into the header field when making a request. So what i was thinking i could do was make a category of NSMutableUrlRequest. Override one of the initalizers. Then in that…
Esko918
  • 1,397
  • 1
  • 12
  • 25
0
votes
0 answers

simply throwing exceptions in static initializers?

I was going through some exercises and got really confused about handling exceptions in static initializers. The online consensus seemed to be: Initializers can only throw unchecked exceptions, or checked exceptions when the exception is also…
whales
  • 787
  • 1
  • 12
  • 24
0
votes
2 answers

How can I find a static class initializer by reflection in C#?

How can I find a static class initializer in C# using reflection? Even GetMembers() invoked on the type of a class does not seem to provide that information.
Regis May
  • 3,070
  • 2
  • 30
  • 51
0
votes
1 answer

java static initializer called twice

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…
0
votes
1 answer

Type variable declared outside the static initialiser within the static initialiser

Could anyone give an concrete example as to what the following text in the JLS (§8.7) means? It is a compile-time error if [...] any type variable declared outside the static initializer, appears anywhere within a static initialiser. And what…
olovb
  • 2,164
  • 1
  • 17
  • 20
-1
votes
3 answers

Can assign a static variable but cannot print it out in a static initializer

I am able to assign value to a static variable but am not able to print it out in the same static block. If I move the static variable above the static block, then all works well. Now I am failing to follow the sequence of execution of the code. The…
Tapiwa
  • 1
  • 1
-1
votes
1 answer

static initializer block return void

I know that it isnt about my problem but just to you know, this is my first stackoverflow post and yes, my english isnt quite good so please, i sincerely ask you guys, be patience. I'd chosen english community because brazillian stack overflow…
FARS
  • 313
  • 6
  • 20
-2
votes
2 answers

Java static initializers

I'm learning java by working through some hacker rank problems. The below code is about learning about static initializer block. The exception is thown and caught but the program keeps running and I am uncertain why. java import…
Abel Asres
  • 17
  • 4
-4
votes
1 answer

When to use initializers?

I recently came across the following bit of java syntax: static { ... } apparently this is known as a "static initializer" (see Static Block in Java) and is "executed when the class is loaded". When should a static initializer be used? What are…
1 2 3 4 5
6