0

I am new to java so when i was debugging a piece of code then i found out that the execution goes to constructor but before entering into the body of constructor it wen to the instance variable so can you please explain me the reason

   public class TestExecution 
    {
       int input1 = 5;
       int input2 = 6;

      TestExecution()
       {
        System.out.println(input1+"before main"+input2);
        int input1 = 10;
        int input2 = 11;
        System.out.println(input1+"after main"+input2);
       }

       public static void main(String[] args) 
       {
        TestExecution testExcution = new TestExecution();
        }
    }

Result was:

 5beforemain6   10aftermain11
Dark Army
  • 268
  • 4
  • 15
  • 1
    you're hiding the instance variables with local variables – Saravana Nov 12 '15 at 07:13
  • @UUID - is right. You are creating new local variables in the constructor. As far your original question goes, the question used to mark it as dup should help – TheLostMind Nov 12 '15 at 07:14
  • @VinodMadyalkar:I just wanted to know why instance variable is executed before constructor variables.I just read the question for which i was duplicated but it dinot answer my question so if you could just help me in comment section then it will be helpful. – Dark Army Nov 12 '15 at 07:20
  • @DarkArmy - Did you check the second part of *meriton's* answer? – TheLostMind Nov 12 '15 at 07:28
  • 1
    @DarkArmy - The fields are initialized before the actually statements of the constructor start executing.. – TheLostMind Nov 12 '15 at 07:32

0 Answers0