Questions tagged [instance-initializers]

6 questions
23
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
  • 44,548
  • 39
  • 95
  • 146
7
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
  • 589
  • 1
  • 7
  • 17
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…
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
  • 916
  • 1
  • 7
  • 9
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
  • 44,548
  • 39
  • 95
  • 146