How to call superconstructor in a neat way

Posted by sandis on Stack Overflow See other posts from Stack Overflow or by sandis
Published on 2010-03-24T14:33:08Z Indexed on 2010/03/24 15:03 UTC
Read the original article Hit count: 255

Filed under:
|
|

So here is my code:

public MyClass (int y) {
    super(y,x,x);
    //some code
}

My problem is that in this case i want to generate a 'x' and sent to the super constructor. However the call to the superconstructor must be the first line in this constructor. Of course I could do something like this:

int x;
{
    x = generateX();
}

But this feels ugly, and then the code will run no matter what constructor I use, which feels not so nice. Right now I am consider encapsulating my whole object in another object that only calculates x and then starts this object. Is this the best approach?

© Stack Overflow or respective owner

Related posts about java

Related posts about best-practices