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
Post a Comment