View RSS Feed

Recent Blogs Posts

  1. Java Generic Methods

    by , 02-18-2012 at 04:55 PM
    It is also possible and supported to generify methods in Java.

    Java Code: This is the code to expalin generic methods
    public static <T> T add(T element, Collection<T> collection){
        collection.add(element);
        return element;
    }
    A type T is being specified by this method. It is used as generic type of collection as well as for element parameter type.

    Java Code:
    String stringElement = "stringElement";
    List<String> stringList = new ArrayList<String>();
    ...
    Categories
    Generics
  2. Generic Methods and Constructors

    by , 02-18-2012 at 04:37 PM
    Within methods declaration of the type parameters could be done and constructor signatures for creation of the generic constructors or generic methods.
    Java Code: This is the code to explain Generic Methods
    public 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: " +
    ...