1

If I have a constructor

public class Sample {


    public static StackOverflowQuestion puzzled;
    public static void main(String[] args) {

        puzzled = new StackOverflowQuestion(4);
    }
}

and inside the main method of a program i have

public class StackOverflowQuestion {

    public StackOverflowQuestion(){
    //does code
    }

    public StackOverflowQuestion(int a){
    this();
   }
}

Is this creating an instance of StackOverflowQuestion via constructor2 and then creating another instance of StackOverflowQuestion via constructor 1 and therefore i now have two instances of StackOverflowQuestion directly inside each other?

Or does constructor2 in this case kind of laterally adjust and then instead create an instance of StackOverflowQuestion via constructor1 ?

2
  • Please post a SSCCE demonstrating your problem/question. What you have here won't compile... Commented Apr 24, 2013 at 8:24
  • @jlordo There is no problem with what he posted. If you replace constructor and constructor2 with StackOverflowQuestion it will compile. Calling them constructor instead was just for clarity. Being able to execute this is not necessary, since it is just a question about how the constructors work. Commented Apr 24, 2013 at 8:32

5 Answers 5

4

I think you mean:

public class StackOverflowQuestion
{

    public StackOverflowQuestion(){ // constructor
       //does code
    }

    public StackOverflowQuestion(int a){ // another constructor
       this();
    }
}

And call it like:

StackOverflowQuestion puzzled = new StackOverflowQuestion(4);

This will only create one object, because new is executed only once. The call this() will execute the code in the other constructor without creating a new object. The code in that constructor is able to modify the currently created instance.

Sign up to request clarification or add additional context in comments.

1 Comment

To be more precise, each call to this() adds a stack frame; does not create any additional objects (as answered).
3

It only creates one instance. One use case of it is to give default values for constructor parameters:

public class StackOverflowQuestion
{
    public StackOverflowQuestion(int a) {
        /* initialize something using a */
    }

    public StackOverflowQuestion() {
        this(10); // Default: a = 10
    }
}

Comments

1

this() is not the same as new StackOverflowQuestion()

this(5) is not the same as new StackOverflowQuestion(5)

this() and this(5) calls another constructor in the same class.

Therefore in this example:

public class StackOverflowQuestion
{
    private int x;
    private int y;
    private int a;

    public StackOverflowQuestion(){
       this.x = 1;
       this.y = 2;
    }

    public StackOverflowQuestion(int a){
       this();
       this.a = a;
    }
}

The call to this() will just initialize the object and not create a new instance. Remember new StackOverflowQuestion(5) has been called already invoking the constructor which actually creates a new instance of the StackOverflowQuestion object

2 Comments

Thank you, this example with the added info cleared up one last question I had. I was curious to know if using this() would access the first constructor, and then return to finish code in the previous constructor. Thanks for the help.
@leigero yes the this() call will return to the constructor it was called from. The compiler checks we call this() before any other code in the constructor as the constructor we call this() from may depend on fields being initialized in the other constructor first. Otherwise we get a compile time error.
1

A constructor does not create an object. It just initializes the state of the object. It's the new operator which creates the object. Read through Creation of New Class Instance - JLS. Now what does this mean :

public class StackOverflowQuestion
{

   public StackOverflowQuestion(){ // constructor
      //does code
  }

  public StackOverflowQuestion(int a){ // another constructor
     this();
  }
}

 StackOverflowQuestion puzzled = new StackOverflowQuestion(4);

A new object of StackOverflowQuestion is created by the new operator, just before a reference to the newly created object is returned as the result and assigned to the StackOverflowQuestion puzzled reference variable , the constructor StackOverflowQuestion(int a) makes a call to this() i.e. public StackOverflowQuestion(), code(if any) inside the default constructor runs and the control comes back to `StackOverflowQuestion(int a), the remaining code(if any) inside that is processed to initialize the new object.

Comments

0

The instance of a class is created at the moment you use the "new" operator. It is perfectly possible to create a class without constructors, because in that case, by default, the constructor with no parameters is available.

The "this" keyword just points to THIS specific object, so calling "this()" means "call my own constructor function, the one without parameters and execute what's inside"

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.