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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:10:00+00:00 2026-06-17T19:10:00+00:00

I am building a java class to be used as a generic comparator based

  • 0

I am building a java class to be used as a generic comparator based on the sample shown on Generic Comparator. The constructor I am building for the comparator is as follows:

public GenericComparator(java.lang.reflect.Field sortField, boolean ascending){
        ....   }

While the above constructor allows me to get the sort field, there is no way for the compiler to know if the field is implementing Comparable interface and a developer calling the constructor may pass a field that doesn’t implement the Comparableinterface.

I am wondering if there is a way to make this a compile time error and not handle it at Run time with the use of instanceof check to make sure the field has implemented the interface.

A similar implementation found in the Collections.sort(...) Type declaration.

public static <T extends Comparable<? super T>> void sort(List<T> list) {
    ...
}
  • 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-17T19:10:01+00:00Added an answer on June 17, 2026 at 7:10 pm
    Comparable.isAssignableFrom(field.getType())
    

    does it at runtime, but there’s no way at all to make this a compile-time error. Fields don’t (and can’t, really) carry their type information at compile time — they’re runtime objects, and can be chosen at runtime. For example, if you had

    random.nextBoolean() ? Foo.class.getField("bar") : Foo.class.getField("baz")
    

    then no compiler, no matter how smart, could tell at compile time what type the resulting field would have.

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

Sidebar

Related Questions

I'm currently working on a project that is building a java-based desktop application to
A friend and I are building a fairly complex website based on java. (PHP
I'm using a 'Properties' class in Java which inherits from HashMap . I'm building
I am building a site based completely off REST services with a Java backend.
I'm building a java web media-scraping application for extracting content from a variety of
I'm building a java application which works in a LAN environment, every computer on
I'm building a Java application that has parts that require daily update. I want
I am building a Java SE application that is powered by Hibernate. Mainly many
I'm currently working on building a java web app. I've been looking to use
Friends, I am building a JAVA TCP listener, where it can handle 6000 incoming

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.