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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:18:38+00:00 2026-06-10T08:18:38+00:00

public class Test<T>{ public boolean isMember(T item) { if(item instanceof Test) { return true;

  • 0
public class Test<T>{

    public boolean isMember(T item) {

        if(item instanceof Test)
        {
            return true;
        }
        return false;
    }
}

Is this the correct way to check if the item is an instance of the class?

I went through some searches and it seems that for a generic class, this will not work.

  • 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-10T08:18:39+00:00Added an answer on June 10, 2026 at 8:18 am

    It’s unclear what you’re trying to test here, but here are a few possibilities:

    1. Is item a T? Yes. Otherwise, it presumably couldn’t be passed into the isMember method. The compiler would disallow it. (See Alex’s caveat in the comments below.)
    2. Is item a Test? Your isMember method as it is written would test this, but I’m sensing a code smell here. Why would you expect a T to also be a Test, but only some of the time? You may want to reconsider how you’re organizing your classes. Also, if this is really what you want, then your method could be written as:

      public boolean isMember(T item) {
          return (item instanceof Test);
      }
      

      Which begs the question: why have a method like this in the first place? Which is easier to write?

      if(obj instanceof Test) {...}
      

      or

      if(Test<Something>.isMember(obj)) {...}
      

      I would argue that the first one is simpler, and most Java developers will understand what it means more readily than a custom method.

    3. Is item a Test<T>? There is no way to know this at run time because Java implements generics using erasure. If this is what you want, you’ll have to modify the method signature to be like Mike Myers’s example.

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

Sidebar

Related Questions

public class Test { boolean[][] expectedResults = new boolean[8][2]; expectedResults[0] = new boolean[] {false,
Lets say i have this usercontrol public class test : UserControl { public int
Take a look at this code: public class Test { public static void main(String...
In Groovy code something simple: #!/usr/bin/env groovy public class test { boolean val def
Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object
public class A { public void method(boolean b){ if (b == true) method1(); else
I have this: import javax.swing.*; import java.awt.Canvas; import java.awt.image.BufferedImage; public class test extends Canvas{
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
I have following code public class TEST { public static void main(String arg[]){ try

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.