7

I do not understand why the below code displays the error Constructor call must be the first statement in a constructor if I shift this(1); to the last line in the constructor.

package learn.basic.corejava;

public class A {
    int x,y;

    A()
    {     
        // this(1);// ->> works fine if written here
        System.out.println("1");
        this(1);  //Error: Constructor call must be the first statement in a constructor
    }

    A(int a)
    {
        System.out.println("2");
    }

    public static void main(String[] args) { 
        A obj1=new A(2);  
    }   
}

I've checked many answers on this topic on StackOverflow but I still could not understand the reason for this. Please help me to make clear about this error with some easy example and explanation.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
beginner
  • 665
  • 6
  • 14
  • 31
  • The reason is that "Constructor call must be the first statement in a constructor". So inside a constructor, the call to `this(...)` must be the first instruction. If it comes after `System.out.println("1")`, it's not the first instruction, but the second one. – JB Nizet Nov 25 '13 at 16:40
  • @OldProgrammer i already saw those answer but still could not understand that's why i asked it here again. is this illegal to ask again? – beginner Nov 25 '13 at 16:41
  • Because that is the way the developers of the language designed it. It also makes sense. This ensures that any attributes/behaviors of the parent class are in a proper state prior to executing any other statements in the derived class's constructor. – OldProgrammer Nov 25 '13 at 17:02
  • You should not ask the same question again unless the new question is significantly different. – Kevin Panko Nov 25 '13 at 17:44

2 Answers2

10

As you know, this works:

A() {
      this(1);
      System.out.println("1");
}

Why? because it's a rule of the language, present in the Java Language Specification: a call to another constructor in the same class (the this(...) part) or to a constructor in the super class (using super(...)) must go in the first line. It's a way to ensure that the parent's state is initialized before initializing the current object.

For more information, take a look at this post, it explains in detail the situation.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 2
    Yeah i know that its rule. But i would like to know why this is not valid if i write it at last? I must know it because in Interview in INDIA , my interviewer are asking such questions – beginner Nov 25 '13 at 16:42
  • 1
    As I said above: if you leave the call to `this()` at the end, you run the risk that in the lines above you're referencing state in the superclass that's not initialized yet. This doesn't happen in the code shown, but in the general case the compiler can't be sure of it, so it's not allowed – Óscar López Nov 25 '13 at 16:45
  • 1
    @rani What's the confusion? He answered your question. The language specification says it, so it must be so. – ach Nov 25 '13 at 16:45
  • okay, what i understand is "its a rule of java lanuage, and also it says that "parent's state should be initialized before initializing the current object." , Is this understanding is enough to tell to my interviewer if he asked such question? – beginner Nov 25 '13 at 16:52
  • @rani If you actually understood the concepts you'd be able to figure that out for yourself. – ach Nov 25 '13 at 17:05
1

The error tells you the problem

A()
{     
      System.out.println("1");
      this(1);  //Error: Constructor call must be the first statement in a constructor
}

i.e. you must call the constructor first

A()
{
      this(1);
      System.out.println("1");
}

this also applies to calls to super

class B extends A
{
    B()
    {
        super();
        System.out.println("1");
    }
}

the reason being is answered here

Community
  • 1
  • 1
T I
  • 9,785
  • 4
  • 29
  • 51
  • Thanks for your answer. I read those(http://stackoverflow.com/questions/1168345/why-does-this-and-super-have-to-be-the-first-statement-in-a-constructor) answer but still could not understand thats why i asked here this question. – beginner Nov 25 '13 at 16:48