Passing an generic parameter to the method

java generic method parameter
how to pass generic class object as parameter in java
java generics
java generic type parameter
java pass class type as parameter
java generic method parameter extends
what are generics
java generic method return type

I have a little question about generic-programming in Java. Is there any way to check the type of the parameter passed to the method? I would like to compare the types of an instance which calls the method, and the parameter passed to it.

If they are not the same, the action of the method should be stopped (kind of protection).

public void homeMatch(SportTeam<Type> visitors){

    if(the type of visitors and this-object are not the same){
         //do this
    }
    else{
         //do something different
    }
}

You cannot operate with Type at runtime as it is erased by the compiler. It only exists at the source code for design purposes.

Being more specific, this method signature will be compiled to something like

public void homeMatch(SportTeam visitors)

Generic Methods (The Java™ Tutorials > Bonus > Generics), Notice that we don't have to pass an actual type argument to a generic method. The compiler infers the type argument for us, based on the types of the actual� This is a very good example of how to invoke a generic method when what you have is a runtime type, but the OP isn't actually trying to do this in his example. @Moo-Juice's answer correctly solves the OP's question. Still, +1 because I've come upon this answer multiple times when I needed it for this "runtime type as a generic parameter" scenario.

What you could do, if you really want to have that check, is to add a class parameter to the functions arguments. Than compare the classParameter with the class of this. This would work because visitors have the same generic type as typeClass.

public<Type> void homeMatch(Class<Type> typeClass, SportTeam<Type> visitors){
    if (typeClass.getClass() == this.getClass()){

    }
}

Java - Generics, A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments. I think you need to qualify the generic T better, otherwise you could pass in a double, string, int, object into the constructor and it would not work as expected. – Moop Mar 3 '13 at 23:49 Yes, that would be a very good idea.

if I understand correctly you must use instanceof . like this:

if (visitors instanceof Type) {
      // action
}

Generics in Java, We can also pass multiple Type parameters in Generic classes. the type of arguments passed to generic method, the compiler handles each� Problem summary: I would like to pass a class with a type parameter (such as ArrayList<SomeClass>, for example) to a generic method as a type parameter. Let's say I have a method: public static <T> T getGenericObjectFromJson(String json, Class<T> genericType){ // details unimportant, basically returns an object of specified type return

The Basics of Java Generics, Generic methods have a type parameter (the diamond operator We're passing a function that converts an array with the elements of type T to� A generic method is a method that is declared with type parameters, as follows: static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; } The following code example shows one way to call the method by using int for the type argument:

Generics, Note the different placement of the generic type parameter in generic function The contravariant type parameter of a parameter type (the user is passing a� Generic Methods in C# Using Generics. In the above example, We have duplicated the code MessageBox.Show(val.ToString()) in both the functions. This works correctly, but it is not efficient. If we want to extend the function to handle double data type, then we need to write one more overloaded function repeating the code again.

Java Generics - Class Objects as Type Literals, As you can see the return type changes depending on what class object you pass in as parameter to the method. This can be quite handy in� Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used. It is possible to use both generic methods and wildcards in tandem. Here is the method Collections.copy ():

Comments
  • take a look at instanceOf operator