Home > Software design >  Optional parameters: Multiple constructors cause errors
Optional parameters: Multiple constructors cause errors

Time:01-03

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?

CodePudding user response:

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.

CodePudding user response:

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;
    }
}
  •  Tags:  
  • java
  • Related