0

I have the following Java-class:

public class Permutation {
 ...
private Permutation(int[] cycle){...} //1

private Permutation(int[] inp, int[] outp){...} //2

public Permutation(int[] array, boolean isCycle){ //3
        if(isCycle){
            //creation new Permutation through "1"
        }
        else{
            //splitting on input and output arrays and
            //creation new Permutation through "2"
        }
    }
 ...
}

But I when I tried to realize constructor "3", I cought compile error. I know, that I can realize this logic using static method like this:

public static Permutation createPermutation(int[] array, boolean isCycle){
    if(isCycle){
        return new Permutation(array);
    }
    //else branch...
}

Also I know, that call to this() or super() must be the first statement in the constructor. But is it possible to realize exactly constructor with architecture "3" (not static creator or something else) in my class? And if it is possible, how can I do that?

UPD: It is not duplicate to How do I call one constructor from another in Java? because in my problem I should realize some logic before calling this() method and this is the main part of my problems.

Community
  • 1
  • 1
VeLKerr
  • 2,995
  • 3
  • 24
  • 47
  • 2
    No, that's not possible. But you can create overloaded `private void init(...)` methods with the signatures of your constructor 1 and 2, and then have all 3 constructors invoke these `init` methods. – Erwin Bolwidt Apr 01 '15 at 01:19
  • possible duplicate of [How do I call one constructor from another in Java?](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java) – Robert Moskal Apr 01 '15 at 01:20
  • @RobertMoskal While related, I wouldn't exactly call that a duplicate. – FThompson Apr 01 '15 at 01:21
  • @RobertMoskal, no in [How do I call one constructor from another in Java](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java) this() - is a first statement in constructor, but I want to realize some logic before calling this(). – VeLKerr Apr 01 '15 at 01:27
  • Dunno, I think the solution is to call this() with the right signature in each if clause. But I defer to you. – Robert Moskal Apr 01 '15 at 01:27
  • I suppose since this has to be the first line in the constructor and you can only overcome this limitation with static methods means that it's not possible @Vulcan – Robert Moskal Apr 01 '15 at 01:30
  • @ErwinBolwidt, thanks, init() is good answer for most of the same problems, but in my class some fields declared as final. – VeLKerr Apr 01 '15 at 01:50
  • I think I would utilize a *creational pattern* that encapsulate the logic more nicely than a chain of public & private constructors does. Maybe go for a *builder*...? – Jan Groth Apr 01 '15 at 02:07
  • 1
    Then drop the #1 constructor and change #2: `private Permutation(int[] inp, int[] outp){if (outp == null) { /* code for cycle */ } else { /* code for non-cycle */} ...} //2`. Not that nice but if you want to use constructors and not a static method, and you are using final variables like you describe, then I don't really know another way. – Erwin Bolwidt Apr 01 '15 at 02:10

0 Answers0