简体   繁体   中英

Optional parameters: Multiple constructors cause errors

Since in Java optional parameters are not possible, I tried to create 2 constructors.

public class Tts {
    public Context context;
    private final MediaPlayer _mediaPlayer;
    private final CopyBarVisualizer _barTop;
    private final CopyBarVisualizer _barBottom;

    public Tts(Context context) {
        this.context = context;
    }

    public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
        this.context = context;
        this._mediaPlayer = _mediaPlayer;
        this._barTop = (CopyBarVisualizer) _barTop;
        this._barBottom = (CopyBarVisualizer) _barBottom;
    }
}

Now my problems are the private properties. There I get the error

Variable '_mediaPlayer' might not have been initialized

In another method, I want to check if the properties are set. But how can I avoid these errors?

To resolve the error, you can:

Delete final keyword in attributes

public class Tts {
    public Context context;
    private MediaPlayer _mediaPlayer;
    private CopyBarVisualizer _barTop;
    private CopyBarVisualizer _barBottom;

    public Tts(Context context) {
        this.context = context;
    }

    public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
        this.context = context;
        this._mediaPlayer = _mediaPlayer;
        this._barTop = (CopyBarVisualizer) _barTop;
        this._barBottom = (CopyBarVisualizer) _barBottom;
    }
}

Give a default null value to your final attributes

public class Tts {
    public Context context;
    private final MediaPlayer _mediaPlayer; 
    private final CopyBarVisualizer _barTop; 
    private final CopyBarVisualizer _barBottom; 

    public Tts(Context context) {
        this(context, null, null, null); // to solve the error problem
    }

    public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
        this.context = context;
        this._mediaPlayer = _mediaPlayer;
        this._barTop = (CopyBarVisualizer) _barTop;
        this._barBottom = (CopyBarVisualizer) _barBottom;
    }
}

This code is from @Khahani


For optional attributes in your class you can use Builder pattern :

public class TtsBuilder{
    public Context context;
    private MediaPlayer _mediaPlayer = null;
    private BarVisualizer _barTop = null;
    private BarVisualizer _barBottom = null;

    public TtsBuilder(Context context) {
        this.context = context;
    }

    public TtsBuilder withMediaPlayer(MediaPlayer _mediaPlayer) {
        this._mediaPlayer = _mediaPlayer;
        return this;
    }

    public TtsBuilder withBarTop(BarVisualizer _barTop) {
        this._barTop = _barTop;
        return this;
    }

    public TtsBuilder withBarBottom(BarVisualizer _barBottom) {
        this._barBottom = _barBottom;
        return this;
    }

    public Tts build() {
        return new Tts(context, _mediaPlayer, _barTop, _barBottom);
    }
}

Example:

public static void main(String[] args){
    Tts ttsMedia = new Tts(aContext).withMediaPlayer(aMediaPlayer).build();
    Tts ttsBarTopBottom = new Tts(aContext)
            .withBarTop(aBarTop)
            .withBarBottom(aBarBottom)
            .build();
}

Note

The naming convention in Java recommends:

Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

For more details you can check Oracle documentation .

If you want to solve the error problem, you can fix it with the code below; however, when the constructor initializes final fields, you can't change them, so probably it's better to remove the final keyword.

public class Tts {
    public Context context;
    private final MediaPlayer _mediaPlayer; 
    private final CopyBarVisualizer _barTop; 
    private final CopyBarVisualizer _barBottom; 

    public Tts(Context context) {
        this(context, null, null, null); // to solve the error problem
    }

    public Tts(Context context, MediaPlayer _mediaPlayer, BarVisualizer _barTop, BarVisualizer _barBottom) {
        this.context = context;
        this._mediaPlayer = _mediaPlayer;
        this._barTop = (CopyBarVisualizer) _barTop;
        this._barBottom = (CopyBarVisualizer) _barBottom;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM