lundi 5 octobre 2015

Reflection in nested classes [duplicate]

This question already has an answer here:

I constantly catch NoSuchMethodException TypesForSets$SetType.<init>(int) during attempts to reach out the constructor of the nested class. I have code:

    public class TypesForSets {
        static <T> Set<T> fill(Set<T> set, Class<T> type) {
            try {
                for (int i = 0; i < 10; i++)
                    set.add(type.getConstructor(int.class).newInstance(i));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return set;
        }

         static <T> void test(Set<T> set, Class<T> type) {
                fill(set, type);
         }

         public static void main(String[] args) {
                test(new HashSet<SetType>(), SetType.class);
         }

         class SetType {
            int i;

            public SetType(int n) {
                i = n;
            }
         }
    }

When I put SetType separately as public class it works just fine.





Aucun commentaire:

Enregistrer un commentaire