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

  • Home
  • SEARCH
  • 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 7657253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:58:50+00:00 2026-05-31T12:58:50+00:00

My problem can be summed-up by this snippet: public interface TheClass<T> { public void

  • 0

My problem can be summed-up by this snippet:

public interface TheClass<T> {
    public void theMethod(T obj);
}

public class A {
    private TheClass<?> instance;

    public A(TheClass<?> instance) {
        this.instance = instance;
    }

    public void doWork(Object target) {
        instance.theMethod(target); // Won't compile!

        // However, I know that the target can be passed to the
        // method safely because its type matches.
    }
}

My class A uses an instance of TheClass with its generics type unknown. It features a method with a target passed as Object since the TheClass instance can be parameterized with any class. However, the compiler won’t allow me to pass the target like this, which is normal.

What should I do to circumvent this issue?

A dirty solution is to declare the instance as TheClass<? super Object>, which works fine but is semantically wrong…

Another solution I used before was to declare the instance as raw type, just TheClass, but it’s bad practice, so I want to correct my mistake.

Solution

public class A {
    private TheClass<Object> instance; // type enforced here

    public A(TheClass<?> instance) {
        this.instance = (TheClass<Object>) instance; // cast works fine
    }

    public void doWork(Object target) {
        instance.theMethod(target); 
    }
}
  • 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-31T12:58:51+00:00Added an answer on May 31, 2026 at 12:58 pm
    public class A {
        private TheClass<Object> instance;
    
        public A(TheClass<Object> instance) {
            this.instance = instance;
        }
    
        public void do(Object target) {
            instance.theMethod(target); 
        }
    }
    

    or

    public class A<T> {
        private TheClass<T> instance;
    
        public A(TheClass<T> instance) {
            this.instance = instance;
        }
    
        public void do(T target) {
            instance.theMethod(target);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem can be summed up by making this simple command works : nice
I think that this problem can be sorted using reflection (a technology which I'm
I have a problem - i can't compile SqlCipher. I'm using this http://groups.google.com/group/sqlcipher/browse_thread/thread/55c6296b56bf4533/c792bbec6df7d4f4?tvc=2#c792bbec6df7d4f4 instructions
there's this interesting problem i can not solve myself. I will be very glad,
this must be such a simple problem but can someone tell me why this
This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL)
Got this really stupid problem: Can find the proper name for the android folder
I believe that the Hamiltonian cycle problem can be summed up as the following:
enter code here hello, everyone, can somebody explain what is my problem in this
Problem: How can I tell if a selection of text in the CRichEditCtrl has

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.