Linked Questions

2 votes
1 answer
729 views

Given String className = "com.example.MyClass"; How would I be able to check if this class implements a specific interface? I've tried Class myClass = Class.forName(className); if (myClass ...
Alex Plouff's user avatar
0 votes
1 answer
67 views

I'm a newbie to Java and want to write a method checking if this object is of a sub class of given class <p_SuperClassName>. Eclipse denies compilation of the following code giving the message ...
tswd's user avatar
  • 1
362 votes
24 answers
151k views

I am working on an application and one design approach involves extremely heavy use of the instanceof operator. While I know that OO design generally tries to avoid using instanceof, that is a ...
Josh's user avatar
  • 18.9k
320 votes
4 answers
952k views

What is the 'instanceof' operator used for? I learned that Java has the instanceof operator. Can you elaborate where it is used and what are its advantages?
Nikunj Patel's user avatar
  • 22.1k
266 votes
4 answers
179k views

Let clazz be some Class and obj be some Object. Is clazz.isAssignableFrom(obj.getClass()) always the same as clazz.isInstance(obj) ? If not, what are the differences?
Albert's user avatar
  • 69k
114 votes
5 answers
89k views

I have a Class object. I want to determine if the type that the Class object represents implements a specific interface. I was wondering how this could be achieved? I have the following code. ...
user489041's user avatar
  • 28.4k
51 votes
8 answers
72k views

If I am creating a java class to be generic, such as: public class Foo<T> How can one determine internally to that class, what 'T' ended up being? public ???? Bar() { //if its type 1 ...
dreadwail's user avatar
  • 15.5k
30 votes
9 answers
15k views

In Java, is it possible to attempt a cast and get back null if the cast fails?
shoosh's user avatar
  • 79.4k
24 votes
3 answers
19k views

I would like my BallUserInterfaceFactory to return an instance of a user interface that has the proper generic type. I am stuck in the example below getting the error: Bound mismatch: The generic ...
FuryComputers's user avatar
14 votes
5 answers
17k views

I am just curious to ask this, maybe it is quite meaningless. When we are using instanceof in java, like: if (a instanceof Parent){ //"Parent" here is a parent class of "a" } why we can't use like ...
Sam YC's user avatar
  • 11.8k
8 votes
4 answers
12k views

I want to dynamically tell if an Object in Java is numeric or not. My code is as the following: if(obj.getClass().equals(Number.class)) { attributeTypeStr = "Numeric"; } else { ...
tonga's user avatar
  • 12k
6 votes
5 answers
8k views

I have a colleague here in my project, who is deeply against the use of instanceof operator, because it "generates a lot of overhead", what is the reason for that? Is it true? Is there another way to ...
Cristiano Fontes's user avatar
3 votes
7 answers
14k views

How can I say the following: while(input is not an int){ do this } I tried this code but I know it's wrong: int identificationnumber; Scanner sc3 = new Scanner(System.in); identificationnumber = ...
MosesA's user avatar
  • 965
3 votes
5 answers
5k views

I don't know how to perform type checking on newEntry, I want to make sure that it is of type MyTable (without creating an object of MyTable). public static boolean add(String table, Object newEntry) ...
Don Code's user avatar
  • 896
3 votes
2 answers
33k views

I have a method for which the return type is object. How do I create a test case for this? How do I mention that the result should be an object? e.g.: public Expression getFilter(String expo) { /...
Jessie's user avatar
  • 973

15 30 50 per page