2 Replies Latest reply: Apr 27, 2014 12:29 PM by cksharma RSS

    Generics Help

    cksharma

      Hi, I am writing the following program in Java SE 7. It throwing "Cannot make a static reference to the non-static type String" . However if I write parameterised String inside main method as  java.lang.String[] args, it compiles fine.

       

      class MainClass<String> {

        <T> MainClass(T t) {

         System.out.println(t.getClass().getName());

        }

       

        public static void main(String[] args) {

        System.out.println("asdasd");

         new MainClass<>("");

        }

      }

       

      I mean following programs compile fine in Java SE 7 :

       

      class MainClass<String> {

        <T> MainClass(T t) {

         System.out.println(t.getClass().getName());

        }

       

        public static void main(java.lang.String[] args) {

        System.out.println("asdasd");

         new MainClass<>("");

        }

      }

       

      Help me...