0

Can I set the default arument value in Construct function something like ?

public class XLSReader {
  public XLSReader(String filename="XYZ.xls") {
  }
}

3 Answers 3

3

No. Java doesn't support optional parameters. You can use overloading and chaining though:

public XlsReader() {
    this("XYZ.xls");
}

public XlsReader(String filename) {
    // Use filename here
}

(This applies to methods as well as constructors.)

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

Comments

2

No, you can't. Default parameter is not supported in Java.

Comments

2

No you cannot but what you can do is to have 2 constructors like this:

public class XLSReader {
  String filename;

  // constructor with a filename argument
  public XLSReader(String filename) {
     this.filename = filename;
  }

  // default constructor will fill-in "default value" XYZ.xls
  public XLSReader() {
     this.filename = "XYZ.xls";
  }
}

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.