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 9024597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:03:27+00:00 2026-06-16T06:03:27+00:00

Below code : List<? extends String> genericNames = new ArrayList<String>(); genericNames.add(John); Gives compiler error

  • 0

Below code :

List<? extends String> genericNames = new ArrayList<String>();
genericNames.add("John");

Gives compiler error :

Multiple markers at this line
– The method add(capture#1-of ? extends String) in the type List is not applicable for the
arguments (String)
– The method add(capture#1-of ?) in the type List is not applicable for the arguments (String)

What is causing this error ? Should I not be able to add Strings or its subtype since I am extending String within the type parameter ?

  • 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-06-16T06:03:29+00:00Added an answer on June 16, 2026 at 6:03 am

    When you use wildcards with extends, you can’t add anything in the collection except null. Also, String is a final class; nothing can extend String.

    Reason: If it were allowed, you could just be adding the wrong type into the collection.

    Example:

    class Animal {
    
    }
    
    class Dog extends Animal {
    
    }
    
    class Cat extends Animal {
    
    }
    

    Now you have List<? extends Animal>

    public static void someMethod(List<? extends Animal> list){
        list.add(new Dog()); //not valid
    }
    

    and you invoke the method like this:

    List<Cat> catList = new ArrayList<Cat>(); 
    someMethod(catList);
    

    If it were allowed to add in the collection when using wildcards with extends, you just added a Dog into a collection which accepts only Cat or subtype type. Thus you can’t add anything into the collection which uses wildcards with upper bounds.

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

Sidebar

Related Questions

I have the below java code: List<SomePojo> list = new ArrayList<SomePojo>(); //add 100 SomePojo
I use below code to show folder list in AlertDialog: ListDialog = new AlertDialog.Builder(MyActivity.this);
Im getting the following error for the piece of code below : expression list
The below code works when there are a few element in the To list
I have the following code below for list into master page <div id=header> <ul>
I have the below code, which iterates over a list based on a custom
I wanted to generate a list view using below code. But after running this
I am actually having a list of text boxes. I am using below code
In the code below i`m trying to read a list of selected projects and
I have the below code which is suppose to show a list of contact

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.