• September 6, 2014
  • Generics
  • Comments Off on Generics constructor example with two parameters

Generics constructor example with two parameters

A constructor can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list is identical to a type parameter list of a generic class or interface. The interface constructor is generic.

Generics constructor example with two parameters:

GenericsTest.java

/**
 * This class is used to show the use of 
 * generics constructor with two parameters.
 * @author javawithease
 */
class Test {
	//Generics constructor with two parameters.
	public <T, U> Test(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 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

Download this example.
 
Next Topic: JUnit Tutorial with example.
Previous Topic: Generics constructor example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.