Easy learning with example program codes

Generics class example

Generics class in java and generics interface in java:

Generic classes/interfaces have the class name followed by a type parameter section. Type parameter section of can have one or more type parameters separated by commas. These classes/interfaces are known as parameterized classes/interfaces.


 * This class is used to show the use generics class.
 * @author codesjava
class Test<T> {
	  private T item;
	  public Test(T item){
		  this.item = item;
	  public T getItem() {
		  return item;
	  public void setItem(T item) {
		this.item = item;
	  public void showItemDetails(){
		  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<String> test1 = 
			new Test<String>("Test String.");
		//Integer type test
		Test<Integer> test2 = 
			new Test<Integer>(100);


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.
Next Topic: Generics class example with two parameters.
Previous Topic: Lower bounded wildcard.

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.


Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki