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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:45:05+00:00 2026-06-13T09:45:05+00:00

I am using generics in my class. MyObject must use T as an class,

  • 0

I am using generics in my class. MyObject must use T as an class, e.g. new MyObject(Long.class). In the method equal I want to ensure that the argument given to the method is an instance of T.

Object<T> is not a valid Java Source, I know.
I added it to the code example to show you what I mean, i.e. that value is an instance of T.

But what is the correct way to implement the equal method??

public class MyObject<T extends Class> {

    public boolean equal(Object<T> value) {
        return true;
    }
}
  • 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-13T09:45:06+00:00Added an answer on June 13, 2026 at 9:45 am

    Assuming that you’re not out to override Object.equals(Object):

    T extends Class makes scant sense because Class is final anyway, so the only thing T could be would be Class<U> for some U, and then you might as well let U be the type parameter. So what you might want it something like:

    public class MyClass<T> {
      private Class<T> cl ;
      public MyClass(Class<T> cl) {
        this.cl = cl ;
        ...
      }
      public boolean equal(T value) {
        return true;
      }
    }
    

    and you then get to write, say, new MyClass<Long>(Long.class).

    But are you sure you need to store the cl parameter at runtime at all? Normally you would leave it out completely, so you just write new MyClass<Long>().

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

Sidebar

Related Questions

in java i am using generics and in class i want to use implements
I want to create an instance of any class using generics. Is that possible?
I have a generics class, that uses TBase as the type parameter. Using MEF,
I'm having trouble using generics. Given the following example : class A<T> { public
Suppose I want to convert a class into another using generics. can I do
I'm trying to implement an identity map using generics. I have an abstract class,
I have a mutable class that I'm using as a key to a generic
Is there a way to say this method returns this using Generics? Of course,
I've done a pretty basic class in C++/CLI using generics. How can I check
How can I map a DataReader object into a class object by using generics?

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.