The methods which can handle different types of arguments are known as generic methods. Types of the argument are specified on method call.
Syntax: generic method includes a type parameter, inside angle brackets and appears before the method’s return type.
public static <T> T identical(T obj){
return returnValue;
} |
public static <T> T identical(T obj){
return returnValue;
}
Example:
GenericsTest.java
/**
* This class is used to show the use of generics method.
* @author w3schools
*/
class Test {
//Generics method
public <T> void showItemDetails(T item){
System.out.println("Value of the item: " + item);
System.out.println("Type of the item: "
+ item.getClass().getName());
}
}
public class GenericsTest {
public static void main(String args[]){
//String type test
Test test = new Test();
test.showItemDetails("Test String.");
test.showItemDetails(100);
}
} |
/**
* This class is used to show the use of generics method.
* @author w3schools
*/
class Test {
//Generics method
public <T> void showItemDetails(T item){
System.out.println("Value of the item: " + item);
System.out.println("Type of the item: "
+ item.getClass().getName());
}
}
public class GenericsTest {
public static void main(String args[]){
//String type test
Test test = new Test();
test.showItemDetails("Test String.");
test.showItemDetails(100);
}
}
Output:
Value of the item: Test String.
Type of the item: java.lang.String
Value of the item: 100
Type of the item: java.lang.Integer |
Value of the item: Test String.
Type of the item: java.lang.String
Value of the item: 100
Type of the item: java.lang.Integer
Download this example.
Generics method example with two parameters:
GenericsTest.java
/**
* This class is used to show the use of
* generics method with two parameters.
* @author w3schools
*/
class Test {
//Generics method with two parameters.
public <T, U> void showItemDetails(T itemT, U itemU){
System.out.println("Value of the itemT: " + itemT);
System.out.println("Type of the itemT: "
+ itemT.getClass().getName());
System.out.println("Value of the itemU: " + itemU);
System.out.println("Type of the itemU: "
+ itemU.getClass().getName());
}
}
public class GenericsTest {
public static void main(String args[]){
//String type test
Test test = new Test();
test.showItemDetails("Test String.", 100);
}
} |
/**
* This class is used to show the use of
* generics method with two parameters.
* @author w3schools
*/
class Test {
//Generics method with two parameters.
public <T, U> void showItemDetails(T itemT, U itemU){
System.out.println("Value of the itemT: " + itemT);
System.out.println("Type of the itemT: "
+ itemT.getClass().getName());
System.out.println("Value of the itemU: " + itemU);
System.out.println("Type of the itemU: "
+ itemU.getClass().getName());
}
}
public class GenericsTest {
public static void main(String args[]){
//String type test
Test test = new Test();
test.showItemDetails("Test String.", 100);
}
}
Output:
Value of the itemT: Test String.
Type of the itemT: java.lang.String
Value of the itemU: 100
Type of the itemU: java.lang.Integer |
Value of the itemT: Test String.
Type of the itemT: java.lang.String
Value of the itemU: 100
Type of the itemU: java.lang.Integer
Download this example.