Questions tagged [instance-initializers]
7 questions
25
votes
6 answers
Understanding this warning: The serializable class does not declare a static final serialVersionUID
I have some static initializer code:
someMethodThatTakesAHashMap(new HashMap() {
{
put("a","value-a");
put("c","value-c");}
});
For some reason I am receiving a warning from Eclipse:
The serializable class does not declare a static…

Alex Baranosky
- 48,865
- 44
- 102
- 150
8
votes
5 answers
Why can my instance initializer block reference a field before it is declared?
My understanding is that you cannot reference a variable before it has been declared, and that all code (including instance initializers) that is within the body of a class, but outside of any method, is executed in order before constructor when the…

Anomaly
- 932
- 1
- 10
- 18
1
vote
1 answer
calling a java constructor of same class from non static context leads to recursion but with static it works fine?
I am trying to understand initialization order of Java class. Specifically when and in what order are static and Instance initializer/fields are executed. I came up with example as in this stackoverflow Question. Why does adding static to the self…

Mayank Madhav
- 429
- 1
- 7
- 19
1
vote
4 answers
Swift: How to initialize instance of a class within a class
I am baffled by the errors arising while trying to initialize an instance of an array in a class. The comments below are the errors xcode 6 is showing.
I have created a class. It is having instance of NSMutableArray. I want to initialize the array…

David Johnston
- 976
- 1
- 8
- 10
0
votes
1 answer
Compilation order of instance initializer blocks
Instance initialization blocks execute in the order in which they are defined. Why, then, does the code below have errors where indicated?
public class MyTest {
public static void main(String[] args) {
Tester t = new Tester();
…
user1549195
0
votes
1 answer
How to use an instance initializer with a generic HashMap?
Can you use an instance initializer with a generic HashMap?
I found this code online, but am having trouble converting it to a generic HashMap instead of a basic HashMap:
someMethodThatTakesAHashMap(new HashMap(){{put("a","value-a");…

Alex Baranosky
- 48,865
- 44
- 102
- 150
-1
votes
1 answer
Why are my Java Instance Initializers initializing twice?
I have written a simple code with a super class and two sub classes in order to track order or initialization. My static initializers behave like expected and initialize in the proper order. But my instance initializers seem to run twice before my…

JBee
- 1
- 1