In this tutorial we are going to learn, how to create custom generic class. See the below example.
package sree; public class Student { String name; String id ; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
package sree; public class Sample<T> { T t ; Sample(T t){ this.t =t; } public void print() { System.out.println("Type object is :" + t.getClass().getName() ); } public T get() { return t; } public static void main(String[] args) { Student student = new Student(); student.setId("101"); student.setName("sree"); Sample<Student> sample = new Sample<Student>(student); sample.print(); Student student2 = sample.get(); System.out.println("Student name is : "+ student2.getName()); System.out.println("Student name is : "+ student2.getId()); } } /* ===== Output : ===== Type object is :sree.Student Student name is : sree Student name is : 101