Search code examples
javaclassoopreturngame-engine

Is this a thing in java?


So I want to have a function for my game engine that I am writing in java, where it will return an object of type T. My code for the function goes like:

public <T> T getComponent(Class<T> tClass)
    {
        for(Component component : components)
        {
            if(component.getClass() == tClass)
            {
                return(T)component;
            }
        }
        return(null);
    }

And this works well and I can call it with getComponent(Component.class); However I want to be able to call it like I would with C# (getComponent<Component>();) so I am wondering if this is possible.

Telling me if its possible or not would be great and giving me code to implement it would be even better, thank you for whatever contribution you make, even if its just reading this so that more people will see it.


Solution

  • It seems as thought this is not possible according to the comments so this is now closed.