5

could someone help me with calling the first constructor and putting it in the second and third? I may have some problems with the syntax, it seems...

http://pastebin.com/5x11Mkyy

3
  • Please post the code directly in your question. Commented Sep 21, 2013 at 17:37
  • 2
    Please add a concise example of your issue in the body of the question rather than using a pastebin link. Commented Sep 21, 2013 at 17:37
  • 1
    The syntax for calling one constructor from another (constructor delegation) is indeed different. Use this(otherargs...) Commented Sep 21, 2013 at 17:37

1 Answer 1

16

Your linked example is really long and I'm getting confused by all the non-English comments, so I'll just give you a short example. If you want to call another constructor within a constructor, you just use the this keyword. Here's a sample class that uses this to delegate the work of the "default" (no-arg) constructor to a 1-arg constructor:

public class MyClass {

  public final int X;

  public MyClass() {
    this(1); // Use X=1 by default
  }

  public MyClass(int x) {
    X = x;
  }

}

This technique is covered in Using the this Keyword: Using this with a Constructor in Oracle's Java Tutorials.

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

1 Comment

Ah yes, my apologies for the foreign comment and lengthy paste. I've edited the commentary and put up a new link.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.