views:

146

answers:

3

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?

+2  A: 

What about super(y, generateX(), generateX())

Poindexter
What if `generateX` had side-effects?
Chris Jester-Young
@Chris, I suppose that would just be up to the developer to rectify.
Poindexter
unless `generateX` is static... this is not possible. The compiler will say `Cannot refer to an instance method while explicitly invoking a constructor`
YuppieNetworking
+12  A: 

How about:

public MyClass(int y) {
  this(y, generateX());
  //some code
}

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

private static int generateX() {
  return 10;
}

If you're happy for generateX to be called twice, you don't need the extra constructor - it's just here to allow the same value to be used for both superclass constructor parameters.

Jon Skeet
@Jon Skeet: what's that? did I miss something in java language evolution?
Roman
@Roman: What's what? That's always been valid code. Which bit are you surprised by?
Jon Skeet
You're mixing class declaration with c'tor declaration. It doesn't compile.
BalusC
@Ah, yes. Oops. That's because I copied and pasted from the question. Fixing :)
Jon Skeet
@Jon Skeet: I'm confused with 2 things: 1) where is 'class' key-word between `public` and `MyClass`? 2) never seen construction like `extends AnotherClass(int y)`. Any links? My java compiler doesn't understend it too.
Roman
@Roman: It was just a typo in the question which I propagated without noticing. I'm sure the OP didn't mean to include the "extends SuperClass" bit - or meant to put it in the class declaration :)
Jon Skeet
anyway 6 upvotes look very funny :)
Roman
I'm pretty sure you mean for line 7 to be `super(y, x, generateX());`
Chris
@Jon Skeet: thanks, it seemed for a moment that I missed a huge part in java.
Roman
@Chris: No I didn't. The point was to only call generateX() once from the public constructor, but then use that same value twice in the superconstructor call. That's the only reason for having that private constructor at all :)
Jon Skeet
I guess I'm just questioning why you'd 'generate' something once and then pass it twice in the same method call. Guess it could be the desired behavior, just doesn't sound right. Oh well, not really pertinent to the question anyhow.
Chris
@Chris: The alternative is calling `generateX()` twice - which doesn't have the same semantics, as it could generate different values (e.g. if it's creating random GUIDs).
Jon Skeet
Yes, that would be the implication. We don't know anything about what he's generating, and for better or worse I associate generation with something that doesn't always produce the same result.
Chris
@Chris: Exactly :) The way I viewed the original code suggested that generateX() should only be called once, and the same value passed twice to the parent constructor. Of course, I could have misunderstood :)
Jon Skeet
A: 

A builder pattern may also be relevant here. A bit heavy weight for the given example but if there is further setup to be done on MyClass it should be considered.

David Soroko