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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:52:12+00:00 2026-06-14T02:52:12+00:00

In the following example: public static void main(String[] args) { List<String> b = new

  • 0

In the following example:

public static void main(String[] args) {

    List<String> b = new ArrayList<String>();
    first(b);
    second(b);

    List<List<String>> a = new ArrayList<List<String>>();
    third(a);
    fourth(a);  // doesnt work

}

private static <T> void first(List<T> a){
    System.out.println("List of T");
}

private static void second(List<?> a){
    System.out.println("List of anything ");
}

private static <T> void third(List<List<T>> a){
    System.out.println("List of a List of T ");
}

private static void fourth(List<List<?>> a){
    System.out.println("List of a List of anything ");
}

Why does the call to second(b) work, but the call to fourth(a) doesn’t ?

I get the following error:

The method fourth(List<List<?>>) in the type `TestTest` is not applicable for the arguments (`List<List<String>>`)
  • 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-14T02:52:14+00:00Added an answer on June 14, 2026 at 2:52 am

    If you want to be able to call fourth with a List<List<String>> argument, then you’ll need to change your signature to this:

    private static void fourth(List<? extends List<?>> a){
        System.out.println("List of a List of anything ");
    }
    

    The above will work because unlike List<List<?>>, List<? extends List<?>> is compatible with List<List<String>>. Think of it this way:

    List<List<String>> original = null;
    List<? extends List<?>> ok  = original; // This works
    List<?> ok2                 = original; // So does this
    List<List<?>> notOk         = original; // This doesn't
    
    List<Integer> original      = null;
    List<? extends Number> ok   = original; // This works
    List<?> ok2                 = original; // So does this
    List<Number> notOk          = original; // This doesn't
    

    The reasoning is simple. If you had

    private static void fourth(List<List<?>> a) {
        List<?> ohOh = Arrays.asList(new Object());
        a.add(ohOh);
    }
    

    And then if you could call that method as such:

    List<List<String>> a = new ArrayList<List<String>>();
    fourth(a);
    String fail = a.get(0).get(0); // ClassCastException here!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following example: public static void main(String[] args){ List<String> list = new ArrayList<String>();
class ZiggyTest{ public static void main(String[] args){ List<Integer> list = new LinkedList<Integer>(); list.add(4); list.add(5);
I have the following code: public class Test { public static void main(String[] args)
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
Say suppose I have the following Java code. public class Example { public static
Take the following example, I have a class public class SomeItem { public string
I was trying following example: class BaseClass { public void methodA(Class<?> cl) { System.out.println(Base.methodA());
Lets look at the following example: public class BothPaintAndPrintable implements Paintable,Printable{ public void print()
I was browsing over the following code example: public class GenericTest { public static
Let's have a following example: public class X { } public class Y {

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.