2

Someone wrote a generic class that I wish to test. It looks something like:

public class < E > foo{  
     public static void main(String[] args){
         do_stuff();
     }
}

I have another class which tries to test the above function's main method as follows:

public class Test{
   public static void main(String[] args){
        foo<Integer>.main({"hello"});
   }
}

Why doesn't this work? What is the correct way to do this? Note: It is possible the problem is with their code and not with mine, but I can't confirm this.

user308485
  • 611
  • 1
  • 5
  • 25

1 Answers1

2

First off, the class with the generic type will not compile, as you have to define the type after the name: public class foo<E> (watch your conventions!).

Next, you can use the following to call its method with a specific generic type, however the type is unused in this case:

public class Test {
    public static void main(String[] args){
         foo.<Integer>main(new String[] {"hello"});
    }
}

Your compiler will most likely warn you against doing this though!

Jacob G.
  • 28,856
  • 5
  • 62
  • 116