What will be the result of compilation and execution of the following code:

01:  public class DecTest {
02:     
03:     private static int x = getY();
04:     private static int y = 5;
05:     
06:     private static int getY(){
07:         return y;
08:     }    
09:     
10:     public static void main(String[] args) {
11:         System.out.println(x);
12:     }
13: }
Explanation
Static fields are initialized in the order that they appear in the source code, therefore when x is being initialized, y still has a default value - 0.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести