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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:09:55+00:00 2026-05-22T22:09:55+00:00

Either I’m too stupid to use google, or nobody else encountered this problem so

  • 0

Either I’m too stupid to use google, or nobody else encountered this problem so far.

I’m trying to compile the following code:

public interface MyClass {
  public class Util {
    private static MyClass _this;
    public static <T extends MyClass> T getInstance(Class<T> clazz) {
      if(_this == null) {
        try {
          _this = clazz.newInstance();
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
      return _this;
    }
  }
}

Howerer, in the line “return _this;” I get the error “Type mismatch: cannot convert from MyClass to T”
Why is this? T extends MyClass, so where is the problem? If I change the line to “return (T)_this;”, i just get a warning about the unchecked cast, but I don’t like warnings 😉 Is there a way to achieve what i want without an error or warning?

  • 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-05-22T22:09:55+00:00Added an answer on May 22, 2026 at 10:09 pm

    Imagine you have two implementations of MyClass, Foo and Bar. As a field of type MyClass, _this could be a Foo or a Bar.

    Now, since your getInstance method returns <T extends MyClass>, it’s legal to call it any of these ways:

    MyClass myClass = Util.getInstance(MyClass.class);
    

    This doesn’t work if it’s the first call, because MyClass is an interface and can’t be instantiated with newInstance().

    Foo foo = Util.getInstance(Foo.class);
    Bar bar = Util.getInstance(Bar.class);
    

    Now, what would happen if _this was an instance of Foo and you called Util.getInstance(Bar.class)? That’s why you aren’t allowed to do this.

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

Sidebar

Related Questions

The following question can either be solved by probably changing my use of the
Either i'm going nuts or nobody likes/liked this feature, but a long time ago
Either I've been staring at this code for way too long or I just
Either I had a bad dream recently or I am just too stupid to
Either I missed some backlash or backlashing does not seem to work with too
I need to capture either \d+\.\d+ or \d+ but nothing else. For instance ,
I want to use either a DTD or an XSD to describe my XML
Either I've got the worlds most popular site or theres something wrong with google
Do either of the below approaches use the correct mathematics for rotating a point?
Either this this question isn't asked often (possible), everyone knows how to do it

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.