-1

so this is my code:

public Student(ArrayList<Course> courseList) {
        this.courseList = courseList;
    }
    
    public Student(ArrayList<Mark> reportCard) {
        this.reportCard = reportCard;
    }

and this is the error: Erasure of method Student(ArrayList) is the same as another method in type Student.

the Array lists are different and I've learned that as long as the inputs of the constructors of a class are not similar, i can have as many constructor as i want. so, would you please tell me what's the problem with this code? I would really appreciate that.

I tried to have a constructor which makes all the array lists i want since it wouldn't give any error that way, However i reluzed that it doesn't go with my design. so I HAVE to use seperate constructors for each array list.l

lin
  • 1
  • 1
  • 1
    Does this answer your question? [Constructor with different ArrayList type as parameter](https://stackoverflow.com/questions/21556925/constructor-with-different-arraylist-type-as-parameter) If not, for additional information, try this search: https://stackoverflow.com/search?q=%5Bjava%5D+arraylist+erasure – Old Dog Programmer Aug 21 '23 at 15:45
  • 1
    This sounds like a potential misunderstanding for constructors: they should completely construct the `Student` object _on their own_, ideally without leaving other parts of the class uninitialized. It sounds like your constructor should really take two lists, or perhaps you could access/store the `Mark` list another way. Using an unbounded constructor would be a terrible approach imo, you'd have differently behaving objects based on what's in your list. – Rogue Aug 21 '23 at 15:51
  • 1
    The compiler erases the generic type. So runtime sees two identical constructors. – WJS Aug 21 '23 at 15:54
  • I have to wonder if you have a design flaw, an [XY Problem](https://xyproblem.info/), or misunderstood something. The example raises suspicions, in that you initialize `this.courseList` and not `this.ReportCard` in one case, and vice-versa in the other case. Perhaps you could edit the question to show more code, and more explanation about what you are trying to do, and why. – Old Dog Programmer Aug 21 '23 at 15:54
  • 1
    This typically means you need a new design to contain the data. I would use the _wildcard_ `?`, and then throw an _Exception_ if it isn't a _Course_ or _Mark_. – Reilas Aug 21 '23 at 20:28

0 Answers0