Skip to content
Advertisement

Optional parameters: Multiple constructors cause errors

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

JavaScript

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?

Advertisement

Answer

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.

JavaScript
Advertisement