+ 3

what is the difference between these initializations? class A { private int i; { i = 67; } } and class A { private int i = 67; }

13th Aug 2016, 3:46 PM
Sardor
Sardor - avatar
3 Respuestas
+ 6
The first one is an initializer block. The Java compiler will copy initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors. The second one is an initial value for a field in its declaration. See more different ways of initializing field https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
13th Aug 2016, 4:57 PM
WPimpong
WPimpong - avatar
0
Thanks a lot man 👍
14th Aug 2016, 3:05 AM
Sardor
Sardor - avatar
- 2
your first one you have i set as 67 and set as your variable. your second one you dont. you just have it say 67 and the computer will be "confused"
13th Aug 2016, 4:06 PM
Aquarius
Aquarius - avatar