Linked Questions
28 questions linked to/from How to determine an object's class?
96 votes
6 answers
239k views
Check if an object belongs to a class in Java [duplicate]
Is there an easy way to verify that an object belongs to a given class? For example, I could do if(a.getClass() = (new MyClass()).getClass()) { //do something } but this requires instantiating a ...
55 votes
4 answers
213k views
How to identify object types in java [duplicate]
Possible Duplicate: How to determine an object's class (in Java)? Java determine which class an object is I have following sample incomplete method to compare the object type of a given ...
1 vote
2 answers
10k views
Check if object is of type String [duplicate]
I need to write a new method that checks if certain values are of the type String or not. I have two objects and I want to be able to check if these two objects are strings, if they are to then ...
-1 votes
3 answers
172 views
Java - How to check if a abstract superclass is a particular subclass [duplicate]
The question might look trivial but I couldn't find the solution anywhere! I have an abstract superclass Vehicle, and there are 4 subclasses that extend it, viz Car, Bus, Lorry and Bicycle. I also ...
-1 votes
1 answer
190 views
Java - How to check if the queue of type ArrayBlockedQueue of LinkedList [duplicate]
if my method is accepting Queues from any type, how to determine if the method arg is ArrayBlockedQueue() or LinkedList() queue? I used .equals as the following its always returning true Queue q1 =...
1 vote
1 answer
87 views
Catch a specific object inside an Array of mutiple Objects [duplicate]
I created a class called "Object" and two classes "Item" and "Scenery" that implement the class Object ( every item is an object and every scenery is an object) During my ...
0 votes
1 answer
62 views
Java: Accessing original class of an item inside an array list [duplicate]
Let's assume I have the following classes: public class AnSuperClass { ... } and public class AnBetterClass extends AnSuperClass { ... } Also I have the following ArrayList: private final ArrayList&...
-2 votes
3 answers
58 views
If loop for what type of Object is in a 2d array of Objects [duplicate]
I am currently working on a project, in which I have a maze. The maze is a two-dimensional array of Objects. I have several different objects (player, wall, path, minotaur) which will be in the array. ...
-1 votes
1 answer
45 views
Class object of subclass in super-class method [duplicate]
Following super-class/sub-class relationship: public abstract class A { public Class<? extends A> getSubClass() { Class<? extends A> clazz = ??? } } public class B extends A {...
0 votes
0 answers
44 views
How to check whether an instance of a superclass is a subclass [duplicate]
I am working on a school project that involves a parking lot. Our 'parking lot' contains a 10x10 grid and each space can either contain a vehicle, or be empty. I have a superclass of vehicles and ...
0 votes
0 answers
41 views
How to get name of subclass from an instance of the superclass placed in another class? [duplicate]
for example in the code below I want to get "Dog" or "Cat". How do I do that? public class Animal { // insert code here } public class Dog extends Animal { // insert code here } public ...
0 votes
0 answers
20 views
Can a Arraylist contain subclasses of a class?Check what subclasses type in a Arraylist? [duplicate]
For example, I have class A and its subclasses B and C. I have an Arraylist contains class A's objects. The Arraylist can contain objects from class B and C? I have a foreach loop looping thought the ...
47 votes
13 answers
29k views
Java two varargs in one method
Is there any way in Java to create a method, which is expecting two different varargs? I know, with the same object kind it isn't possible because the compiler doesn't know where to start or to end. ...
4 votes
5 answers
946 views
Is using .getClass() regarded as a bad design?
I am currently implementing a function, using the superclass as a parameter. For example: private void foo(Parent parent) { if(parent.getClass() == Child1.class) { Child1 c1 = (Child1) ...
0 votes
3 answers
13k views
Checking classes with if statement in Java
I was wondering if there is a way to check with the if statement of which class an object is an instance of. I would need something like this: if(object.getClass().equals("class Circle")) ...