Java - Method executed prior to Default Constructor -


this question has answer here:

i'm learning java , accidentally came across following code default constructor executed after method.

 public class chkcons {      int var = getval();      chkcons() {         system.out.println("i'm default constructor.");     }      public int getval() {         system.out.println("i'm in method.");         return 10;     }      public static void main(string[] args) {          chkcons c = new chkcons();      }  } 

output :

 i'm in method. i'm default constructor. 

can please explain me why happened?

thanks.

instance variable initialization expressions such int var = getval(); evaluated prior constructor execution. therefore getval() called before constructor executed.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -