Keanu Hie
Keanu Hie

Reputation: 307

How to cast Java class to another class

I use a method to collect all classes from a package and save them into an array:

Class[] classes = ClassUtils.getClasses("xyz.keahie.werewolf.character.characters");

They all have the class "Character" as superclass and when I print them I get this line:

class xyz.keahie.werewolf.character.Character

When I want to cast them to the class "Character" I always get an ClassCastException. What am I doing wrong?

Edit: Here is the full Error which I get:

Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to xyz.keahie.werewolf.character.Character
    at xyz.keahie.werewolf.character.CharacterManager.<init>(CharacterManager.java:29)
    at xyz.keahie.werewolf.GameManager.<init>(GameManager.java:26)
    at xyz.keahie.werewolf.Game.main(Game.java:9)

And here my source code:

Class[] classes = ClassUtils.getClasses("xyz.keahie.werewolf.character.characters");
for (Class clazz : classes) {
    Object object = clazz.getSuperclass();
    System.out.println(object.toString());

    if (object.getClass().equals(xyz.keahie.werewolf.character.Character.class)) {
        System.out.println("Yes");
    } else {
        System.out.println("Nope");
    }

    Character character = (Character) object;
    if (!this.characters.contains(character)) {
        addSpecialCharacter(character);
    }
}

Sorry for the missing information at the begin

Upvotes: 0

Views: 16792

Answers (2)

Michael
Michael

Reputation: 44130

You cannot cast from Class<Character> to Character. The former describes the class of the latter.

You can use Class.newInstance to get an instance provided Character has a constructor which takes no arguments:

Character character = (Character) object.newInstance();

Alternatively, you can use reflection if your Character class requires constructor arguments. See this question: Instantiate a class object with constructor that accepts a string parameter?

I would carefully rethink your software design if need to instantiate an object from it's Class<?> because it is very rarely required.


Be careful with naming your class Character. java.lang.Character may conflict. java.lang is always automatically imported so you have two classes with the same name that are conflicting. It's good practice to avoid using any class names that are already present in java.lang for that reason.

To avoid this, you can fully qualify the class with the package name, like so:

xyz.keahie.werewolf.character.Character

Preferably, choose a less ambiguous name for your class. GameCharacter, PlayerCharacter or RpgCharacter might be better choices.

Upvotes: 5

zhh
zhh

Reputation: 2406

You can't cast a Class<Character> to Character. Your code

Character character = (Character) object;

should be

Class<Character> characterClass = (Class<Character>) object;

It looks like you want to create instance from its class. I recommend you to learn something about Java reflection. Maybe something like this Creating an instance using the class name and calling constructor

Upvotes: 1

Related Questions