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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:53:08+00:00 2026-05-18T01:53:08+00:00

The whole generics thing is kinda throwing me for a loop, and more so

  • 0

The whole generics thing is kinda throwing me for a loop, and more so the RTT.

Specificis? Ah well here’s the gist:

enum QueryHelper {
  query1,
  query2;
  static <T> QueryHelper getQueryHelper (Class<T> expectedReturn) {
    if (expectedReturn.isInstance (SomeRelatedClass.class))
      return query1;
    else
      return query2;
  }
}

and then I would call it like so:

...
QueryHelper helper = QueryHelper.getQueryHelper(SomeRelatedClass.class);
...

This is so that I can really flexibly assign the query return type in the actual helper. It does some casting and object creation. What I am seeing is that there is no match, should I be doing this some other way? Or is the whole idea just bad?

And the real heart of this is that I don’t understand the difference between class.isInstance and the instanceOf operator? Should I be using the latter?

  • 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-18T01:53:09+00:00Added an answer on May 18, 2026 at 1:53 am

    This is so that I can really flexibly assign the query return type in the actual helper.

    There is nothing flexible about the return type of this method

    static <T> QueryHelper getQueryHelper (Class<T> expectedReturn) {
        if (expectedReturn.isInstance (SomeRelatedClass.class))
          return query1;
        else
          return query2;
    }
    

    It will always return an instance of QueryHelper. If you want the return type to be flexible you would need to define it as something like:

    static <T> T getQueryHelper (Class<T> expectedReturn) {
    }
    

    Now the return type is flexible, because it will depend on the type of the argument

    And the real heart of this is that I don’t understand the difference between class.isInstance and the instanceOf operator?

    The difference is that instanceof does a type-check that is fixed at compile-time, for example:

    static boolean isInstance(Object myVar) {
        return (myVar instanceof Foo);
    }
    

    will always check that myVar is an instance of Foo, whereas

    static <T> boolean isInstance(Object myVar, Class<T> expectedType) {
        return expectedType.isInstance(myVar);
    }
    

    will check that myVar is an instance of expectedType, but expectedType can be a different type each time the method is called

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

Sidebar

Related Questions

I'm having a problem with scala generics. While the first function I defined here
The whole thing works perfectly, except image won't show, no errors, Using RoR. What
This whole Facebook access_token thing is driving me nuts. All I want to do
I'm new at this whole Django thing and as such, I've decided to start
the whole point of this is to have transparent text with a light white
My whole app is serving stuff in utf-8, only one page (xml) should return
The whole code is written in ANSI C, and it should remain so. I
The whole point of rvm is to be able to install multiple rubies and
My whole purpose of this is to get something looking like this: I want
Having searched a whole lot of similair posts, workarounds, I decided to make my

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.