0

I have an abstract class Medium, where one of the datamembers is an enum.

private Taal talenOndertiteling[];

public enum Taal {
    NEDERLANDS, FRANS, DUITS, ENGELS, SPAANS, ITALIAANS
}
public Taal[] getTalenOndertiteling() {
    return talenOndertiteling;
}

public void setTalenOndertiteling(Taal[] talenOndertiteling) {
    this.talenOndertiteling = talenOndertiteling;
}

Now when I try to call the last method like this:

            BD bd1 = new BD();
            bd1.setTalenOndertiteling(Taal.ENGELS);

I'm getting an error. (The BD class implements the Medium class) Any ideas on how I should be calling the method? And what if I wanted to set several languates, how would I do that?

Thanks!

3
  • (Note, you almost certainly want to be cloning that array on the way in and out, or better copy a Collection.) Commented Jul 19, 2010 at 14:39
  • What exactly do you mean by cloning? And why would a collection be better? I don't see the difference between using an array or something else (ArrayList?) if I'm only using it as a parameter for the method. Thank you for mentioning this! Commented Jul 19, 2010 at 14:50
  • Not cloning means that your "private" variable is mutable from the outside. This can lead to obscure bugs (and in my line of work, expensive security vulnerabilities). Collections offer a nicer interface (apart from []), rather than an exposed implementation technique. For instance, you probably (though not necessarily, I can't tell from the use of arrays) want the array to behave like a set, so Set would be an obvious thing to write. Commented Jul 19, 2010 at 15:12

5 Answers 5

6

Your method accepts a Taal[] but your invocation passes a Taal. You have two choices:

Either pass an explicit Taal[]:

bd1.setTalenOndertiteling(new Taal[] {Taal.ENGELS});

or, change the method declaration to take a vararg parameter and have the compiler do it for you:

public void setTalenOndertiteling(Taal... talenOndertiteling) {
Sign up to request clarification or add additional context in comments.

6 Comments

I consider the most elegant way to allow one parameter use is to use vararg, which makes a far cleaner code, with no feature loss.
There's very few reasons not to. One reason I can think of is if you intend to pass a literal null parameter, the method invocation is ambiguous. That's somewhat annoying, but I agree that it's almost always a good idea.
@Riduidel - only if if makes sense to accept multiple parameters (you've got to write the implementation after all), when it's the obvious option anyway. For example, on the canonical Person class, what would you gain from declaring setAge(int... age)? What would it mean to call setAge(11, 72, 43)?
@Andrzej Doyle: I interpreted his comment to mean "instead of accepting an array", not to mean that every method should accept varargs.
Oh well, you're obviously right. But I was talking of that particular case. unfortunatly, it was not obvious enough from my sentence. However, i could be a very fun way to have the <code>setAge(int... age)</code> to have multiple events fired (in a swing application, as an example) :-)
|
2

You have to create an array with one element and pass it to the method:

bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});

Comments

0

The setter expects an array of Taal enums.

So the proper use is:

BD bd1 = new BD();
bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});

or more verbose:

BD bd1 = new BD();

Taal[] taals = new Taal[1];
taals[0] = Taal.ENGELS;

bd1.setTalenOndertiteling(taals);

Comments

0

The setTalenOrdertiteling gets an array. So the correct call should be:

bd1.setTalenOndertiteling(new Taal[] {Taal.ENGELS});

Comments

0

two issues: 1) setTalenOndertiteling() is expecting array of Taal, so

bd.setTalenOndertiteling(new Taal[]{Taal.ENGELS};

2) Taal seems to be a nested enum (in BD?), without specific import, you need to do

bd.setTalenOndertiteling(new BD.Taal[]{BD.Taal.ENGELS};

1 Comment

Actually Taal is a datamember from my Medium class. BD is only extending Medium. Thanks for answering!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.