1

Coming from a C++ background, I was hoping to write few util static methods which could be used throughout my application without necessarily going via object creation. And I was hoping to use the equivalence of function templates in Java Generics. At this point, I want to clarify that I am learning Java generics.

But on trying that, I could see that the compiler would not allow me to do so. And I could see some very good discussion as well here. But then I played around with the warning message a little bit to write an inner static class with similar touch, and as per my intuition it worked there. Now obviously, it is clumsy to access inner classes which I have presented below. I wonder why this has been designed this way in Java and good will this inner class offer with generics to outside classes.

public class ReflectionBasics<X, Y> {

    public static void findMax (X xData, Y yData ){
       // compiler error - cannot reference static type to non-static type X 
    }

    public void findMin(X xData, Y yData){
        // this is fine
    }

    static final class InnerClass<E> {
        public void findMin(E data){
            // this will work
        }
    }
}

Clumsy Accessor:

class AccessorClass{
    AccessorClass(){
        ReflectionBasics.InnerClass<Integer> myData = new ReflectionBasics.InnerClass<Integer>();
        myData.findMin(400);

    }
}
Community
  • 1
  • 1
Suparna
  • 1,132
  • 1
  • 8
  • 20
  • 2
    https://docs.oracle.com/javase/tutorial/java/generics/methods.html Java has generic methods. Also note that Java generics are not templates and can not do many things that C++ templates can do. (On the other hand, they can do a few things that C++ templates *can't* do.) – Radiodef Mar 27 '16 at 04:20

1 Answers1

2

If you want to put type parameters on a method, you just stick them before the return type (which you probably didn't want to be void):

public static <T extends Comparable<T>> T findMax(T a, T b) {
    ...
}

In most cases, you can skip the type parameters at the call site, and they'll be inferred for you:

Integer max = WhateverClass.findMax(integerA, integerB);
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • The correct declaration is `public static > void findMax(T a, T b) {` – Vladimir Petrakovich Mar 27 '16 at 04:29
  • @fRoStBiT: Bah, that's what I get for trying to reproduce the awkward syntax from memory. I guess it makes sense to introduce the type parameter before the return type, which could depend on that parameter. – user2357112 Mar 27 '16 at 04:33
  • @fRoStBiT - I thank you for responding on a postive note to my query. I also thank whoever has down voted this question, although I really pity his thought process. This question was not meant for him anyway. I am sure many new java programmers like me will have such genuine question. I can make it that many people did not know the answer. The first person to respond was someone who had 58.9K rep and he was 26th visitor. – Suparna Mar 27 '16 at 05:17