views:

77

answers:

5

Hey, 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!

+2  A: 

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

bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});
codymanix
+6  A: 

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) {
Steven Schlansker
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.
Riduidel
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.
Steven Schlansker
@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
@Andrzej Doyle: I interpreted his comment to mean "instead of accepting an array", not to mean that **every** method should accept varargs.
Steven Schlansker
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) :-)
Riduidel
Oh right. Sorry, that's quite obvious in retrospect, pardon my misinterpretation. I thought it was analogous to the proposed practice that any method that's declared as returning `void` should instead `return this` to allow method chaining. Interesting, but possibly obscuring readability.
Andrzej Doyle
A: 

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);
Andreas_D
A: 

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

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

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};
Adrian Shum
Actually Taal is a datamember from my Medium class. BD is only extending Medium. Thanks for answering!
Sled