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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:03:31+00:00 2026-06-05T16:03:31+00:00

The Generic Methods tutorial has this helpful example: public <T extends E> boolean addAll(Collection<T>

  • 0

The Generic Methods tutorial has this helpful example:

 public <T extends E> boolean addAll(Collection<T> c); 

However, […] the type parameter T is used only once. The return type
doesn’t depend on the type parameter, nor does any other argument to
the method (in this case, there simply is only one argument). […]
If that is the case, one should use wildcards.

The codebase of the project I am working on has a few methods like this:

public <T extends Something> T getThing();

and (not in the same interface)

public <D> void storeData(int id, D data);

Is there any point in having the method type parameter instead of using the bound (Something above, Object below) directly?

(Note that in the former case, all of the few implementations are annotated with @SuppressWarnings("unchecked") and the point could be to hide this warning from the user of the method, but I am not sure this is a laudable achievement.
In the latter case, some implementations use reflection to store instances of different classes differently, but I do not see how this is facilitated by 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-05T16:03:32+00:00Added an answer on June 5, 2026 at 4:03 pm

    There are five different cases of a type parameter appearing only once to consider.

    1) Once in return type position:

    1.a) Return type is the type variable

    public <T extends Something> T getThing();
    

    This should be a red flag: The caller can arbitrarily choose an expected return type and the callee has no way of knowing the chosen type. In other words the implementation can’t guarantee the returned value will be of the specified return type unless it (a) never returns, (b) always throws an exception or (c) always returns null. In all of these cases the return type happens to be irrelevant altogether.

    (Personally I don’t mind methods like these if the code is very “dynamic”. I.e. you’re running the risk of a class cast exception anyway and the method boundary is still early enough for the exception to be raised. A good example is deserialzation. All parties calling the method have to know and understand this though..)

    1.b) Type variable is contained in return type, but not the return type itself

    Very common and valid. Examples are the various factory methods in guava, like Lists.newArrayList().

    2) Once in parameter type position:

    2.a) Simple type parameter

    public static <E> void shuffle(List<E> list);
    

    Note that the implementation actually needs the type parameter in order to shuffle the elements. Nonetheless, the caller should not have to be bothered with it. You can write an internal helper method that “captures” the wildcard:

    public static void shuffle(List<?> list) {
      shuffleWithCapture(list);
    }
    
    private static <E> void shuffleWithCapture(List<E> list) {
      // implementation
    }
    

    2.b) Type parameter with multiple bounds

    public static <T extends Foo & Bar> void foo(T);
    public static <E extends Foo & Bar> void foo(List<E>);
    

    Since Java does not allow intersection types anywhere but in type parameter bounds, this is the only way to express these signatures.

    2.c) Type parameter bound contains the type parameter variable

    public static <T extends Comparable<? super T>> void sort(List<T> list);
    

    To express that the elements of the list must be comparable with each other, one needs a name for their type. There is no way to eliminate such type parameters.

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

Sidebar

Related Questions

Implemented a generic repository with several Methods. One of those is this: public IEnumerable<T>
With generic methods it's possible to extend more than one type, e.g.: <T extends
I'm not really familiar with creating generic methods, so I thought I'd put this
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
How can I make this java generic cast ? public interface IField { }
I'm having trouble grasping generic methods. I have two classes that are generated (they
How to create a generic method which can call overloaded methods? I tried but
We've created a generic method like so: public TReturnType GetValue(string key) { var configurationParameter
I have a generic method define as below public T MyMethod<T>(extra params) My method
I'd like to create a generic method for converting any System.Enum derived type to

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.