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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:24:47+00:00 2026-06-06T17:24:47+00:00

I have a Class with generic type: class Foo<T> { protected boolean validateType(Object obj){

  • 0

I have a Class with generic type:

class Foo<T> {
    protected boolean validateType(Object obj){
        if (obj instanceof T) {
            return true;
        }
        return false;
    }

}

To test:

Foo foo = new Foo<String>();
foo.validateType(new String()); // should return true
foo.validateType(new Long()); // should return false;

And have a function that needs to validate an object against the generic type ‘T’, obviously the code above have errors?

How can I do such

  • 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-06T17:24:49+00:00Added an answer on June 6, 2026 at 5:24 pm

    What you are trying to do is not possible at it stands – generics are implemented in Java by erasure, which means that the generic parameter does not exist at runtime. It’s there for the compiler to typecheck, but at runtime there’s no way to tell the difference between a Foo<String> and a Foo<Long>.

    So if you want a specific token to check against, you’ll have to do this yourself. A common pattern to do this sort of checking is to use Class object, with the benefit that these can be type-checked by the compiler:

    class Foo<T> {
        private final Class<T> genericClass;
    
        public Foo(Class<T> clazz) {
            this.genericClass = clazz;
        }
    
        protected boolean validateType(Object obj){
            return genericClass.isInstance(obj);
        }
    }
    

    Note that your callers now need to pass in the Class object themselves; there’s no way around this requirement, it can’t be automatically wired in by Java itself.

    (Also, it’s a bad idea to use if (x) return true; else return false; when it’s exactly the same as return x; but more confusing and error prone.)

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

Sidebar

Related Questions

Lets say I have a generic class: class Foo { // protected Type t;
I have a Base class that needs a Generic type. That can be either
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a generic class that takes a type T . Within this class
I have a parameterized generic class X which takes a type T. On which
If have a type hierarchy that starts out with a generic type class A<T>
Assume I have a static generic class. Its generic type parameters are not available
I have a Interface Foo which has a generic type - public interface Foo<T>
I have a generic class that I'm trying to implement implicit type casting for.
I have a problem applying the DebuggerDisplay attribute on a generic class: [DebuggerDisplay(--foo--)] class

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.