Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6106703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:06:30+00:00 2026-05-23T14:06:30+00:00

Can someone explain why the following doesn’t work? It complains that: The method add(C)

  • 0

Can someone explain why the following doesn’t work? It complains that:

The method add(C) in the type List is not applicable for the arguments (Generics.Person)

import java.util.ArrayList;
import java.util.List;

public class Generics<C extends Comparable<C>> {

  static class Person implements Comparable<Person> {
    public final String name, city;

    public Person(String name, String city) {
      this.name = name;
      this.city = city;
    }

    @Override
    public int compareTo(Person that) {
      return 0;
    }
  }

  public Generics() {
    List<C> persons = new ArrayList<C>();
    persons.add(new Person(null, null));
  }

  // however, this one works, but it gives a warning
  // about Comparable being a raw type
  public Generics() {
    List<Comparable> persons = new ArrayList<Comparable>();
    persons.add(new Person(null, null));
  }
}

So, basically, what I want is a generic List of Comparables, to which I can add any type that implements Comparable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:06:30+00:00Added an answer on May 23, 2026 at 2:06 pm

    To be allowed to do what you need to do you should specify the type parameter of the Generics class from outside:

    Generics<Person> generic = new Generics<Person>();
    

    Otherwise you are just adding a Person to a list which has an unbound type variable, this is not allowed. While using the Generics class with a free type variable you must not make any assumption on the type of C.

    To see a clear example think about having a second class Place extends Comparable<Place>. According to what you are trying to do you should be allowed to do the following:

    public Generics() {
        List<C> persons = new ArrayList<C>();
        persons.add(new Person(null, null));
        persons.add(new Place());
      }
    

    since also Place is a valid candidate. Then which would be the type of type variable C? Mind that there is no unification process that tries to guess the right type of C according to what you are adding to the list, and finally you should see C not as a “whatever type as long as it fulfills the constraints” but as a “specified type that fulfills the constraints”,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain why the following doesn't work? int main() // Tried on several
Can someone explain why the following doesn't compile? I want that BB[A] is also
Can someone explain why the following doesn't work as I expect? Pressing the button
Can someone please explain why the following doesn't work works: function displayResults(data) { $(#odNextPage).click(function()
Can someone please explain me why the following doesn't work, or give me a
Can someone help explain the following: If I type: a=`ls -l` Then the output
Can someone explain why the following line of C# doesn't behave the same as
I am hoping that someone can explain the following behaviour. Suppose I am trying
Can someone please explain why the ' . $ending . '! doesn't work the
Can someone please explain to me why the following code doesn't draw anything, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.