Reputation: 1676
PersonenGenerator{
Set<Person> personSet = new HashSet<>();
public void printPersonTypeSet(Class clazz){ //pass a class that inherits from the class Person as a parameter
for(Person instance : personSet){
if (instance instanceof clazz) System.out.println(instance);
} // check if instance of Class Person are stored in personSet. If so, print said instance
}
public class Student extends Person{
...
}
public class Employee extends Person{
...
}
public class Main {
public static void main(String[] args) {
PersonenGenerator pg = new PersonenGenerator();
pg.Student s1 = new Student(...);
pg.personSet.add(s1);
pg.Employee e1 = new Employee(...);
pg.personSet.add(e1);
printPersonTypeSet(Employee) //pass Employee as parameter SOMEHOW
}
}
EXPECTED BEHAVIOUR (Output):
Employee{value1, value2, value3}
My Compiler does not like the if-statement for some reason. Specifically where i call clazz
.
ACTUAL BEHAVIOR (Compiler Error) :
Unknown Class:'clazz'
My problem is that I intend to use clazz
as a variable for instances of Person
, while Person
is the superclass for various subclasses.
How do I keep the intended functionality and satisfy my compiler? First time doing this.
EDIT: I feel like I've been misunderstood. I want to pass a class as the parameter under which the condition is checked. I commented the code to clarify this.
If passing the parameter as type Class
is bullshit for this, just say so. It was an idea, because I don't know how to pass a class as parameter.
Upvotes: 1
Views: 122
Reputation: 6252
You want clazz.isAssignableFrom(instance.getClass())
. This returns true
if instance
has type clazz
or is a subtype of type clazz
.
Edit: here's a more complete example. You have a Person
class and some subclasses:
class Person {
String name;
Person(String n) { name = n; }
public String toString() { return name; }
}
class GoodPerson extends Person {
GoodPerson(String n) {
super(n);
}
}
class BadPerson extends Person {
BadPerson(String n) {
super(n);
}
}
Then you use your code with my suggested edit:
public void printPersonTypeSet(Class<?> clazz) {
for (Person instance : personSet)
if (clazz.isAssignableFrom(instance.getClass()))
System.out.println(instance);
}
If personSet
is initialized as:
Set<Person> personSet = new HashSet<Person>();
personSet.add(new GoodPerson("Good1"));
personSet.add(new GoodPerson("Good2"));
personSet.add(new BadPerson("Bad1"));
then you can find the instances of GoodPerson
by using printPersonTypeSet(GoodPerson.class)
. (Note this will also find subclasses of GoodPerson
.)
Upvotes: 2
Reputation: 2972
The best would be using the isInstance(Class)
method. According to the Java documentation This method is the dynamic equivalent of the Java language instanceof operator.
public void printPersonTypeSet(Class clazz){
Set<Person> personSet = ImmutableSet.of(new Person());
for(Person instance : personSet){
if (clazz.isInstance(instance.getClass())) System.out.println(instance);
}
}
public static void main(String[] args) {
new Scratch().printPersonTypeSet(Person.class);
}
From Java doc:
Determines if the specified {@code Object} is assignment-compatible with the object represented by this {@code Class}. This method is the dynamic equivalent of the Java language {@code instanceof} operator. The method returns {@code true} if the specified {@code Object} argument is non-null and can be cast to the reference type represented by this {@code Class} object without raising a {@code ClassCastException.} It returns {@code false} otherwise.
Method Signature
public native boolean isInstance(Object obj)
Upvotes: 1
Reputation: 412
In your example clazz
is an instance of Class
, and that's probably not what you want to give as parameter in your method. So you're asking your compiler if instance
is an instance of clazz
, which it can't be as clazz
is not a class but an instance.
Upvotes: 1