Mercer Mercer - 2 months ago 6
Java Question

Call specific constructor

In my class i have two constructor

public class Acc implements Serializable {
public Acc() {
public Acc(final Pays paysDefaut) {
if (paysDefaut != null) {

My question is when i do

this.accAd = new ArrayList<Acc>(Voon.NB__MAX);

I want to call this constructor
public Acc(final Pays paysDefaut)
. It's possible ...?


No, you can't do it the way you showed in your comment. You can use whatever constructor you like to add elements to the list but not inside the type declaration of a generic.

Simply do this.accAd = new ArrayList<Acc>();

and then

this.accAd.add(new Acc(paysDefaut));

Of course, since you declared your list as List<Acc>, the type can be inferred using the diamond operator (<>) in Java 7+.