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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:07:59+00:00 2026-06-13T19:07:59+00:00

I am reading generics chapter in Thinking in Java 4th edition and there is

  • 0

I am reading generics chapter in “Thinking in Java 4th edition” and there is an example:

class Fruit{}
class Apple extends Fruit {}   

…

static <T> void writeExact(List<T> list, T item) {
            list.add(item);
    }

    static List<Apple> apples = new ArrayList<>();
        static List<Fruit> fruit = new ArrayList<>();

        static void f1() {
            writeExact(apples, new Apple());
            // writeExact(fruit, new Apple()); // Error:
            // Incompatible types: found Fruit, required Apple
        }

        static <T> void writeWithWildcard(List<? super T> list, T item) {
            list.add(item);
        }

        static void f2() {
            writeWithWildcard(apples, new Apple());
            writeWithWildcard(fruit, new Apple());
        }

Those commented line, where error is indicated, doesn’t produce any error at all both in 6 and 7 Java. It seems weird for me, as writeExact method accepts only exact type of parameter. So why does it work? And since it works what is a purpose of supertype wildcards?

edit

so i got it and want to clarify one more thing:
who is the boss in type inference with wildcards? return type, first parameter, second parameter, … . I mean that if SomeClass.<Fruit>method(…) is not specified

  • 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-13T19:08:01+00:00Added an answer on June 13, 2026 at 7:08 pm

    It works because the T in writeExact is a type variable on the method itself. Look, it’s right there before the void return type:

    static <T> void writeExact(List<T> list, T item)
    

    That means that whenever you call it, its T takes on the type needed to accept its arguments, as long as there is one. You can call it with any list and an object that would fit in that list.

    Perhaps it makes it clearer to explicitly give the type argument, rather than letting the compiler infer it (you need to use the class’s name to do this for a static method; i will assume it is MyClass):

            MyClass.<Apple>writeExact(apples, new Apple());
            MyClass.<Fruit>writeExact(fruit, new Apple());
    

    In the second call, T has the value Fruit, which means the first argument must be a List<Fruit>, which it is, and the second must be a Fruit, which it is, because Apple is a subtype of Fruit.

    The rules for implicitly assigning type arguments are given in section 15.12.2.7 of the Java Language Specification. At least, i think they are; it’s not exactly an easy read.

    I would guess that the code in the book is not exactly the same as the code you give above. If it is, then it it definitely incorrect, and you should write to Mr Eckel and tell him.

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

Sidebar

Related Questions

I am reading Thinking in java 4th edition and I encountered this code class
I am reading Thinking in java , Generics. In one example(at paragraph The action
I've been reading Bruce Eckel's Thinking In Java and in the chapter on generics,
I'm reading book the C# programming Language, 4th Edition, by Anders Hejlsberg etc. There
I am reading the chapter on Generics in Effective Java. Help me understand difference
Reading the Scala by Example book and there is this example when Martin explains
Ok, so I was reading the Java online tutorial for generics and I found
I'm reading Generics in the Java Programming Language by Gilad Bracha and I'm confused
I am reading the java tutorial about Wildcards in Generics. In the following code:
I mostly use Java and generics are relatively new. I keep reading that Java

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.