Generic Methods and Constructors
by , 02-18-2012 at 03:37 PM (662 Views)
Within methods declaration of the type parameters could be done and constructor signatures for creation of the generic constructors or generic methods.
The output from this program is:Java Code: This is the code to explain Generic Methodspublic class Box<T> { private T t; public void add(T t) { this.t = t; } public T get() { return t; } public <U> void inspect(U u){ System.out.println("T: " + t.getClass().getName()); System.out.println("U: " + u.getClass().getName()); } public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); integerBox.add(new Integer(10)); integerBox.inspect("some text"); } }
Java Code: Output of above programT: java.lang.Integer U: java.lang.String









Email Blog Entry
License4J 4.0
Today, 12:23 AM in Java Software