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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:21:01+00:00 2026-06-12T16:21:01+00:00

I was reviewing someone else’s code the other day and I came across a

  • 0

I was reviewing someone else’s code the other day and I came across a line that raised some concern. To simplify, say I have a generic Class A and an abstract Class B. Is the following instantiation allowed and if so, why?

Object obj = new A<? extends B>();

I personally have never seen an instantiation like the above, although a declaration such as

A<? extends B> obj = null;

would certainly hold. I’ve always used the wildcard in generics to declare method parameters, so I may just not have the experience.

  • 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-12T16:21:02+00:00Added an answer on June 12, 2026 at 4:21 pm

    Actually new A<? extends B>() does not compile. It has been consistently illegal since Java 5.

    But I guess your original example was something like new A<X<? extends B>>(). The latter is legal in recent versions of Java.

    The idea is, when instantiating an object, the value for type parameters can be any non-wildcard type. ? extends B is a wildcard type, so it is disallowed. But X<? extends B> is not a wildcard type, though it has a wildcard type as a component. So you can say legally call new A<X<? extends B>>().

    The rules makes sense if you think about it this way. Ultimately it is a byproduct of the more fundamental rule that a wildcard type like ? extends B cannot be the declared type of a field or variable. If A is defined as

    class A<T> {
        T value;
    }
    

    then the hypothetical new A<? extends B>().value would be a field declared of type ? extends B. Since that is illegal, so is the instantiation. But new A<X<? extends B>>() does not have that problem.

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

Sidebar

Related Questions

When reviewing code I came across something odd, someone had read that you can
I'm reviewing someone else's C++ code for our project that uses MPI for high-performance
I was recently reviewing some Javascript code in a jQuery plug-in and came across
I'm reviewing a .NET project, and I came across some pretty heavy usage of
I recall reviewing someone else's PHP code once and he had a function or
I was reviewing some code and I saw someone do a if (0 ==
I was just reviewing some code and noticed someone had marked member as readonly
I'm reviewing some code on the project I recently joined, and in a C#
I'm reviewing some code from one of our contractors: if (userLists != null) {
I'm doing some work for a charity that's having a fund drive. Whenever someone

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.