4

I am getting an error with this following code fragment

The error is : cannot reference x before supertype constructor has been called (and pointing out the statement at comment 1)

class Con{
    int x =10;

    Con(){
        this(++x); //1
        System.out.println("x :"+x);
    }

    Con(int i){
        x=i++;
        System.out.println("x :"+x);
    }
}

In the main method I have this statement

    Con c1=new Con();

I don't understand the error. Can someone explain what is actually happening here?

6
  • 1
    I means what it says. You cannot reference x until you've called super(). Normally that happens implicitly, but since you have a this call the super call is suppressed, hence the message. I can't think of a way to do what you want, without restructuring a bit. Commented Aug 11, 2013 at 12:53
  • What is the use of calling this() inside a constructor? Can Somebody explain? I can think of situations with parametrized constructors calling this()... Commented Aug 11, 2013 at 13:00
  • 1
    @boxed__l this() is used call overloaded constructors present in the same class.Assume you have a called constructor which takes 4 parameters, but for the 2 fields there is already another constructor to do initialization, in this case you need to use this(x,y) to call your two argument constructor Commented Aug 11, 2013 at 13:00
  • @Algorithmist :Does Compiler add super() to all constructors (overloaded/empty)? if so, wont' it result in multiple super() calls? Commented Aug 11, 2013 at 13:16
  • 1
    @boxed__l Learning should be continued even if OP accepts an answer,try to gain as much from every question.Regarding your question Yes compiler always adds super() calls if you haven't specified anything and this is valid for parametrized constructors also.If you explicitly called super() then it won't add super() from its side. Commented Aug 11, 2013 at 13:26

3 Answers 3

5

When creating an instance of a class, the constructor first calls it's super class constructor to initialize the super class fields. Once all the super class constructors have run, then only the current constructor continues to initialize it's own field.

Now, when you add a this() call in your constructor, it doesn't call the super class constructor. This is because, the first statement in a constructor is either a chain to super class constructor - using super(), or a different constructor of the same class - using this().

So, you can't pass the field in this(), because the field is isn't initialized yet. But it doesn't really make sense, why you are trying to do something like that?

Remember, the compiler moves the field initialization code inside each constructor of your class. So, your constructor is effectively equivalent to:

Con() {
    this(++x); //1

    // This is where initialization is done. You can't access x before it.
    x = 10;
    System.out.println("x :"+x);
}

This is true even with super() call. So, the below code will also give you the same error (considering Con extends another class with a parameterized constructor):

Con() {
    super(++x); //1
    System.out.println("x :"+x);
}
Sign up to request clarification or add additional context in comments.

6 Comments

since I don't extend any class can we say super class constructor to the parameterized constructor?
@chathura2020. When your class don't extend any class, you need to call the Object class 0-arg constructor, using super().
@chathura2020. All your classes extend at least one class. So, you can't say, your class doesn't extend any other class. If it doesn't do it explicitly, Object class is implicitly the super class.
super(++x); doesn't work because Object class does not have a paramterized constructor :). any ways gr8 answer
@RohitJain : in the above code, if we use "static int x=10;" , then it compiles fine. That means the static variable is initialized before calling the super class constructor. So the order is like this -> Initialize static var(loading class Con), initialize super class, initialize sub class. Correct me if I am wrong.
|
2
Con(){
    this(++x); //1
    System.out.println("x :"+x);
}

At this very moment, Con does not yet exist. It first instantiates by calling the other constructor. That means that x does not exist yet (it is created as soon as the other constructor instantiates). So you can't reference it yet.

If you really need to reference it, you have to use a static variable

private static int x = 10;

Comments

0

First call inside a constructor can only be this() or super() , if their is none of them then compiler automatically insert a call to super but in your constructor you called other constructor by using this() . basically whenever you construct an object the superclass is first initialized then the subclass's members gets initialized.So you can not refer to uninitialized members as they gets initialized after superclass's members and superclass itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.