1

I've searched online and cannot find an answer. Is it improper to assign an argument to more than one variable in a constructor or does there have to be a one-to-one relationship?

public A6HangmanLogic(String keyPhrase, int numberOfGuesses)
{
    this.keyPhrase = keyPhrase;
    this.numberOfGuesses = numberOfGuesses;
    guessesLeft = numberOfGuesses;
}

The value of guessesLeft (a static int) is returned from an accessor method updated through a loop. I can certainly write a mutator method, but then I have to call it. I could also change this.numberOfGuesses to just guesses and not have that third variable assignment at all.

1
  • It's fine. However if you find you're getting too much code in your constructor, just create another method and call that. Commented Feb 24, 2013 at 21:33

3 Answers 3

2

It's fine, but I would store guessCount and use the default initualized value of zero:

private guessCount;

I would also rename numberOfGuesses to maxGuesses so it's clearer what it means (it's also more conventional).

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

Comments

1

There is no reason why you can't assign the same value to multiple variables.

Another way to solve the problem though might be to store the number of guesses made and then calculate the number of guesses left.

Comments

0

Yes, it's perfectly fine to assign an argument to as many variables as you like.

You are allowed to use them in a similar way as you would in a method.

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.