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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:02:20+00:00 2026-06-11T18:02:20+00:00

I have a class with a couple of methods that convert from one generic

  • 0

I have a class with a couple of methods that convert from one generic type to another, but I want to return the original value if both: the argument type and the return type are the same. The best option I could come up with was to cast the argument type and wrap it in a try catch statement, but that seems dirty to me.

public WK wrapKey(K key) {...


public abstract class MyClass<K, V extends Comparable, WK, WV> extends BaseClass<Map<WK, WV>, KeyValueBean<K, V>> {

...
public WK wrapKey(K key) {

    try {
        return (WK) key;
    } catch (Exception e) {
        return null;
    }
}

public WV wrapValue(V value) {

    try {
        return (WV) value;
    } catch (Exception e) {
        return null;
    }
}

I thought the other option could be to specify the class type as an argument in the constructor or a setter method, and then use isAssignableFrom:

public WV wrapValue(V value) {
  if (value.class.isAssignableFrom(this.getWrappedValueClass())){
     return WK(V);
  }
  return null;
}

Is there any other way??

  • 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-11T18:02:22+00:00Added an answer on June 11, 2026 at 6:02 pm

    Java implements generics through type erasure, which means all type checks are performed at compile-time and then the generics are erased at runtime. Because of this, your code below actually does nothing at all:

    public WV wrapValue(V value) {
        try {
            return (WV) value;
        } catch (Exception e) {
            return null;
        }
    }
    

    At runtime, this is equivalent to:

    public Object wrapValue(Object value) {
      return value;
    }
    

    This method will never throw an exception, instead the caller of this method might, because the compiler adds an implicit cast at the call site.

    Your second example won’t even compile because you cannot do value.class. You would have to pass in the class types for both V and KV if you want to do this check.

    In any case, your design seems overly complicated — perhaps if you explain what you are trying to do, there might be a better solution.

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

Sidebar

Related Questions

So I have a couple methods in my class that return NSDictionary has a
I have a abstract base C# class with a couple of methods that has
I have a couple of ListPreferences that my PreferenceActivity displays. From the class that
I have a couple of standard ASP.NET web methods that I'm calling from javascript
I have a couple of classes, all derived from the same base type. class
Consider situation when we have a class with couple of static methods that use
I have a Python class that raises NotImplementedError for a couple of methods and
I have only one class with many instances. Every instance is observer of couple
I have a couple of functions inside a class that essentially do the same
I have a couple extension methods that handle serialization of my classes, and since

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.