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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:44:17+00:00 2026-05-27T01:44:17+00:00

I have my own generic class which goes like this: C1<T> { T value;

  • 0

I have my own generic class which goes like this:

C1<T> {
    T value;
    ...
    void setValue(T val) {
        this.avlue = val;
    }
}

Somewhere else i have this collection:

  List<C1> list;

Then in a different piece of code I instantiate some objects of type C1 like this:

C1<Integer> a;
C1<Float> b;
C1<String> c;
...

Later i put them in the list;

list.add(a);
list.add(b)
...

The next thing i do is this:
iterate over list doing this:

list.get(i).setValue(obj);

where obj is a variable of type Object which could be an Integer, Float, etc.

Now there problem is that is the instance of C1 returned from the list (lets call it c1) was defined like this:

C1<Type1>

Java will allow this:

c1.setValue(val2);

where val2 is of type Type2 and Type2 is not equal to Type1 and is not even in the same class hierarchy (except Object ofc)!

EDIT:
Forgot to ask the question 🙂
How do i make setValue “Type safe” when accessed the way I’m doing it above?

  • 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-27T01:44:17+00:00Added an answer on May 27, 2026 at 1:44 am

    If you store objects of types C1<Type1> and C1<Type2> in a List<C1>, it is not possible to determine the exact type when you retrieve an element from the list. You will only know that it is a C1 (as all elements in the list).

    If you really need to ask the C1 at runtime what its type parameter is, you need to store some type token in C1:

     C1<T> {
        private final Class<T> cls;
    
        C1(Class<T> pCls) {
          cls = pCls;
        }
    
        Class<?> getTypeParameter() {
          return cls;
        }
      }
    

    Then you can call getTypeParameter() on the instance you got from the list and compare it to the type you expected.

    Note that this will still allow you to store values of Type2 in a C1<Type1> if you use an unchecked cast. If you want to eliminate this possibility, check the runtime type of val against cls in setValue() and throw an exception if they don’t match.

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

Sidebar

Related Questions

I have a Generic List object which hold below table as its own value.
I have own project and i would like add for this class same as
I'm trying to hide window after its startup. I have own window-class which is
I'm using my own generic abstract class (MyAsyncTask) which extends AsyncTasks, and need a
This is a little contrived, but say I have a class interface like this:
I have own component which works in my testing winform app good but when
I would like to make every EditText object have own title like in Pure
I have my own installer program which I use to install several applications I
I need a generic collection class which I can add to, and enumerate over.
I have a C# class which needs to process a sequence of items (

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.