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.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes