1

I have two constructors, which the first one creates some process, like create new objects and without if\for statements. and this constructor cannot be changed, even to private.

But the second constructor get an int value and works like if negative throw exception if positive do some process but if num=0 to avoid duplication of code I want to call first constructor.

What is the right method to implement code like this? Note that these constructors are in the same class.

public class SomeClass{

  public SomeClass() {
    //Do some process
  }

  public SomeClass(int num) {  
    if (num == 0) {
        this(); //here we have an issue
    }
  }
}
7
  • 2
    Calling a constructor of the current class must be the first statement in another of its constructors. Commented Dec 14, 2018 at 14:51
  • You might want to move the code within the first constructor into a new private method. Then call this method from the 2nd constructor in case num=0 Commented Dec 14, 2018 at 14:53
  • @Arnaud I know that, that`s the reason I have post this question. I cannot call from the first line because I need check if num is equal to zero. only in this case I want call a first constructor. Commented Dec 14, 2018 at 14:53
  • 1
    firstConstructor and secondConstructor cannot both be constructors since they cannot both have the same name as the class. Commented Dec 14, 2018 at 14:53
  • @luk2302 I guess OP knows that. I edited the question so things are more clear Commented Dec 14, 2018 at 14:56

3 Answers 3

3

Create some kind of factory:

public class SomeClass
{
  // This is the original constructor
  public SomeClass()
  {
    // Do some process
  }

  // New constructor expecting num > 0
  private SomeClass(int num)
  {
    // Do whatever you want
  }

  public static SomeClass getIt(int num) throws Exception
  {
    if (num < 0)
      throw new Exception("...");
    else if (num > 0)
      return (new SomeClass(num));
    else
      return (new SomeClass());
  }

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

5 Comments

Thanks, Why you declare the second constructor as a private? It should be public
I like this solution, however we have missing a little thing. if I call getIt method from the second constructor, and we have num > 0, we call again and again the same constructor. and I think you missed return statement in getIt method but this is correctable.
To answer to "Why you declare the second constructor as a private?"; private member of a class can be accessed by any method of the class static or non-static.
The getIt method is supposed to be called from outside the class, not by the 2nd constructor. I don't see the missing return statement though.
The constructor is private in order to force the user of the class to call the factory method.
2

A call to another constructor (this();) must be first in the constructor.

You may want to refactor the code, putting the "processing" logic in a method:

public Constructor() {
    this.processConstructor();
}

public Constructor(int num) {
    if(num == 0) {
        this.processConstructor();
    }
}

private void processConstructor() {
    //Do some process
}

2 Comments

You beat me to it ! :) If I were you, I would change the name of processConstructor() to something like executeProcess(), just for the sake of comprehension of the solution.
As I said in the post, I cannot change any of the processing logic in the first constructor. I can only edit the second and create new methods. so in this case, this is not useful. please note that the first constructor called only when num equal to zero, in others cases i have another logic process.
0

Do it the other way round:

public class SomeClass{

  public SomeClass() {
    this(1 /* or some sensible default */);
  }

  public SomeClass(int num) {
    // Do stuff.
  }
}

As a general principle, you should only to have one constructor which actually "does useful stuff". The other overloads then only exist to provide reasonable default values to invoke that constructor.

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.