views:

47

answers:

4

Hello there, Im having trouble with some Java, How do I give in default parameter values in java?. for example I have this in c++

DVD(int i, string t, int y, string d="Unknown"): Items(i,t,y),director(d){} and in Java I tried

public Dvd(int i, String t,int y, String d="Unknown"){
        super(i,t,y);
        director = d;
    }

which fails to build. So how do I go about giving in default values?

also In my main testing class I tried giving in 3 arguments insead of 4 but this fails also. How do I get around this problem?.

+3  A: 

Java does not have the ability to specify default parameter values, however you can achieve the same effect by doing something like this, (where you want a2 to have a default of true in this case)

public void aMethod(boolean a1)
{
    aMethod(a1, true);
}

public void aMethod(boolean a1, boolean a2)
{
    // your code here
}

So in your case, you should just overload the Dvd class constructor to have a version without the parameter, and one with the parameter, where the one without passes the default value to the one with.

Rich Adams
+1  A: 

May be you can overload the constructor and try it again

public Dvd(int i, String t,int y, String d ){
        super(i,t,y);
        director = d;
    }


public Dvd(int i, String t,int y){
        super(i,t,y);
        director = "Unknown";
    }
Kunal
+4  A: 

Java does not support default argument construct like that, unfortunately. The traditional way to implement it, for better or worse, is to use what is called "telescoping" methods.

Here's a quote from Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters:

Traditionally, programmers have used the telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameters, a third with two optional parameters, and so on...

The telescoping constructor pattern is essentially something like this:

public class Telescope {
    final String name;
    final int levels;
    final boolean isAdjustable;

    public Telescope(String name) {
        this(name, 5);
    }
    public Telescope(String name, int levels) {
        this(name, levels, false);
    }
    public Telescope(String name, int levels, boolean isAdjustable) {       
        this.name = name;
        this.levels = levels;
        this.isAdjustable = isAdjustable;
    }
}

And now you can do any of the following:

new Telescope("X/1999");
new Telescope("X/1999", 13);
new Telescope("X/1999", 13, true);

You can't, however, currently set only the name and isAdjustable, and leaving levels at default. You can provide more constructor overloads, but obviously the number would explode as the number of parameters grow, and you may even have multiple boolean and int arguments, which would really make a mess out of things.

As you can see, this isn't a pleasant pattern to write, and even less pleasant to use (What does "true" mean here? What's 13?).

Bloch recommends using a builder pattern, which would allow you to write something like this instead:

Telescope telly = new Telescope.Builder("X/1999").setAdjustable(true).build();

Note that now the parameters are named, and you can set them in any order you want, and you can skip the ones that you want to keep at default values. This is certainly much better than telescoping constructors, especially when there's a huge number of parameters that belong to many of the same types.

So Java does not have default argument mechanism, but the builder pattern is a much better idiom anyway.

See also

Related questions

polygenelubricants
thank you very much.
sil3nt
A: 

As others mentioned, Java does not have default parameter values, and this is overcome by creating multiple constructors.

However, there is a special construct called varargs which would allow you to have the equivalent of default values, but it can only be used for the last parameters in the method call, and all of them have to be of the same type.

There's also the builder pattern which can be used to achieve a similar result, e.g.: new Dvd().setI(i).setY(y)...

JRL