Error “illegal generic type for instanceof” when using local classes





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







31















I have the following Java code that uses a local class.



import java.util.Arrays;

public class X<T> {
void m() {
class Z {}

for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {}
}
}


It does not compile with the following error message:



X.java:8: error: illegal generic type for instanceof
if (o instanceof Z) {}
^
1 error


I understand that the local class Z inherits the generic type signature of X<T>, being an inner class. The same kind of compilation error appears in this example, where Z is not local, but still inner:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compilation error
}
}


It can be worked around either by making Z non-inner / static:



import java.util.Arrays;

public class X<T> {
static class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compiles now
}
}


Or by qualifying X.Z:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3)) {
if (o instanceof X.Z) {} // Compiles now
if (o instanceof X<?>.Z) {} // Also
}
}
}


But how can I qualify a local class, or work around this limitation, without changing the local class itself?










share|improve this question

























  • I think this is a kind of compiler pathology. It's a strange way of telling you the type is not reifiable. It's even worse in Eclipse - it tells you "Use the form Z instead".

    – RealSkeptic
    15 hours ago






  • 1





    @RealSkeptic: Eclipse's compiler message is just "unlucky" here. It is usually a helpful extra information.

    – Lukas Eder
    15 hours ago






  • 2





    A corollary of this is that Z array = new Z[0]; is also illegal.

    – Andy Turner
    14 hours ago




















31















I have the following Java code that uses a local class.



import java.util.Arrays;

public class X<T> {
void m() {
class Z {}

for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {}
}
}


It does not compile with the following error message:



X.java:8: error: illegal generic type for instanceof
if (o instanceof Z) {}
^
1 error


I understand that the local class Z inherits the generic type signature of X<T>, being an inner class. The same kind of compilation error appears in this example, where Z is not local, but still inner:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compilation error
}
}


It can be worked around either by making Z non-inner / static:



import java.util.Arrays;

public class X<T> {
static class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compiles now
}
}


Or by qualifying X.Z:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3)) {
if (o instanceof X.Z) {} // Compiles now
if (o instanceof X<?>.Z) {} // Also
}
}
}


But how can I qualify a local class, or work around this limitation, without changing the local class itself?










share|improve this question

























  • I think this is a kind of compiler pathology. It's a strange way of telling you the type is not reifiable. It's even worse in Eclipse - it tells you "Use the form Z instead".

    – RealSkeptic
    15 hours ago






  • 1





    @RealSkeptic: Eclipse's compiler message is just "unlucky" here. It is usually a helpful extra information.

    – Lukas Eder
    15 hours ago






  • 2





    A corollary of this is that Z array = new Z[0]; is also illegal.

    – Andy Turner
    14 hours ago
















31












31








31


1






I have the following Java code that uses a local class.



import java.util.Arrays;

public class X<T> {
void m() {
class Z {}

for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {}
}
}


It does not compile with the following error message:



X.java:8: error: illegal generic type for instanceof
if (o instanceof Z) {}
^
1 error


I understand that the local class Z inherits the generic type signature of X<T>, being an inner class. The same kind of compilation error appears in this example, where Z is not local, but still inner:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compilation error
}
}


It can be worked around either by making Z non-inner / static:



import java.util.Arrays;

public class X<T> {
static class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compiles now
}
}


Or by qualifying X.Z:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3)) {
if (o instanceof X.Z) {} // Compiles now
if (o instanceof X<?>.Z) {} // Also
}
}
}


But how can I qualify a local class, or work around this limitation, without changing the local class itself?










share|improve this question
















I have the following Java code that uses a local class.



import java.util.Arrays;

public class X<T> {
void m() {
class Z {}

for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {}
}
}


It does not compile with the following error message:



X.java:8: error: illegal generic type for instanceof
if (o instanceof Z) {}
^
1 error


I understand that the local class Z inherits the generic type signature of X<T>, being an inner class. The same kind of compilation error appears in this example, where Z is not local, but still inner:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compilation error
}
}


It can be worked around either by making Z non-inner / static:



import java.util.Arrays;

public class X<T> {
static class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3))
if (o instanceof Z) {} // Compiles now
}
}


Or by qualifying X.Z:



import java.util.Arrays;

public class X<T> {
class Z {}

void m() {
for (Object o : Arrays.asList(1, 2, 3)) {
if (o instanceof X.Z) {} // Compiles now
if (o instanceof X<?>.Z) {} // Also
}
}
}


But how can I qualify a local class, or work around this limitation, without changing the local class itself?







java generics local-class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 28 mins ago









Peter Mortensen

13.9k1987114




13.9k1987114










asked 15 hours ago









Lukas EderLukas Eder

137k74448987




137k74448987













  • I think this is a kind of compiler pathology. It's a strange way of telling you the type is not reifiable. It's even worse in Eclipse - it tells you "Use the form Z instead".

    – RealSkeptic
    15 hours ago






  • 1





    @RealSkeptic: Eclipse's compiler message is just "unlucky" here. It is usually a helpful extra information.

    – Lukas Eder
    15 hours ago






  • 2





    A corollary of this is that Z array = new Z[0]; is also illegal.

    – Andy Turner
    14 hours ago





















  • I think this is a kind of compiler pathology. It's a strange way of telling you the type is not reifiable. It's even worse in Eclipse - it tells you "Use the form Z instead".

    – RealSkeptic
    15 hours ago






  • 1





    @RealSkeptic: Eclipse's compiler message is just "unlucky" here. It is usually a helpful extra information.

    – Lukas Eder
    15 hours ago






  • 2





    A corollary of this is that Z array = new Z[0]; is also illegal.

    – Andy Turner
    14 hours ago



















I think this is a kind of compiler pathology. It's a strange way of telling you the type is not reifiable. It's even worse in Eclipse - it tells you "Use the form Z instead".

– RealSkeptic
15 hours ago





I think this is a kind of compiler pathology. It's a strange way of telling you the type is not reifiable. It's even worse in Eclipse - it tells you "Use the form Z instead".

– RealSkeptic
15 hours ago




1




1





@RealSkeptic: Eclipse's compiler message is just "unlucky" here. It is usually a helpful extra information.

– Lukas Eder
15 hours ago





@RealSkeptic: Eclipse's compiler message is just "unlucky" here. It is usually a helpful extra information.

– Lukas Eder
15 hours ago




2




2





A corollary of this is that Z array = new Z[0]; is also illegal.

– Andy Turner
14 hours ago







A corollary of this is that Z array = new Z[0]; is also illegal.

– Andy Turner
14 hours ago














4 Answers
4






active

oldest

votes


















20














To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.



The referenced type in an instanceof expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.



If you declare Z as generic, the instanceof operator treats Z as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.






share|improve this answer


























  • It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

    – RealSkeptic
    15 hours ago











  • "oversight" - You're here to pick a fight with the JLS designers, right? :)

    – Lukas Eder
    15 hours ago






  • 2





    Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

    – Rafael Winterhalter
    15 hours ago











  • I extended my answer to explain why generification of Z does the trick.

    – Rafael Winterhalter
    15 hours ago











  • @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

    – Andy Turner
    14 hours ago





















14














A possible workaround is to use reflection:



import java.util.Arrays;

public class X<T> {
void m() {
class Z {}

for (Object o : Arrays.asList(1, 2, 3))
if (Z.class.isInstance(o)) {}
}
}





share|improve this answer































    2














    Apparently, by making Z generic compilation succeeds. I expected that to require <T> as the type parameter, but you just have to make it generic, so anything will do



    import java.util.Arrays;

    public class X<T> {
    void m() {
    class Z<Anything> {}

    for (Object o : Arrays.asList(1, 2, 3))
    if (Z.class.isInstance(o)) {}
    }
    }


    Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.






    share|improve this answer





















    • 2





      Once you make Z generic, you don't need to apply the reflection workaround anymore...

      – Lukas Eder
      15 hours ago



















    0














    This should work either. Using reflection too. But seems a valid solution.



    import java.util.Arrays;

    public class X<T> {


    void m() {

    class Z2 {
    }

    for(Object o: Arrays.asList(1,2,3)) {
    if(Z2.class.isAssignableFrom(o.getClass())) {

    }
    }

    }

    }





    share|improve this answer


























    • I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

      – Lukas Eder
      15 hours ago












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55703849%2ferror-illegal-generic-type-for-instanceof-when-using-local-classes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    20














    To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.



    The referenced type in an instanceof expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.



    If you declare Z as generic, the instanceof operator treats Z as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.






    share|improve this answer


























    • It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

      – RealSkeptic
      15 hours ago











    • "oversight" - You're here to pick a fight with the JLS designers, right? :)

      – Lukas Eder
      15 hours ago






    • 2





      Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

      – Rafael Winterhalter
      15 hours ago











    • I extended my answer to explain why generification of Z does the trick.

      – Rafael Winterhalter
      15 hours ago











    • @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

      – Andy Turner
      14 hours ago


















    20














    To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.



    The referenced type in an instanceof expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.



    If you declare Z as generic, the instanceof operator treats Z as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.






    share|improve this answer


























    • It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

      – RealSkeptic
      15 hours ago











    • "oversight" - You're here to pick a fight with the JLS designers, right? :)

      – Lukas Eder
      15 hours ago






    • 2





      Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

      – Rafael Winterhalter
      15 hours ago











    • I extended my answer to explain why generification of Z does the trick.

      – Rafael Winterhalter
      15 hours ago











    • @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

      – Andy Turner
      14 hours ago
















    20












    20








    20







    To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.



    The referenced type in an instanceof expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.



    If you declare Z as generic, the instanceof operator treats Z as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.






    share|improve this answer















    To me this seems to be an oversight or limitation in the Java language and I do not think it is possible.



    The referenced type in an instanceof expression must be reifiable according to JLS 4.7, meaning that it must be expressed as a reifiable type by its fully qualified name. At the same time, JLS 6.7 states that local classes do not have a fully qualified name, they can therefore not be expressed as reifiable.



    If you declare Z as generic, the instanceof operator treats Z as a raw type where all generic properties to it - in this case the enclosing class - are considered raw as well. (Similar to a generic methods of a raw type being considered as raw despite any generic signature. This is a measure to retain backwards compatiblity of type generification.) Since any raw type is reifiable, declaring Z to be generic will compile.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 14 hours ago

























    answered 15 hours ago









    Rafael WinterhalterRafael Winterhalter

    28.5k1368149




    28.5k1368149













    • It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

      – RealSkeptic
      15 hours ago











    • "oversight" - You're here to pick a fight with the JLS designers, right? :)

      – Lukas Eder
      15 hours ago






    • 2





      Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

      – Rafael Winterhalter
      15 hours ago











    • I extended my answer to explain why generification of Z does the trick.

      – Rafael Winterhalter
      15 hours ago











    • @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

      – Andy Turner
      14 hours ago





















    • It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

      – RealSkeptic
      15 hours ago











    • "oversight" - You're here to pick a fight with the JLS designers, right? :)

      – Lukas Eder
      15 hours ago






    • 2





      Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

      – Rafael Winterhalter
      15 hours ago











    • I extended my answer to explain why generification of Z does the trick.

      – Rafael Winterhalter
      15 hours ago











    • @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

      – Andy Turner
      14 hours ago



















    It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

    – RealSkeptic
    15 hours ago





    It's true, but curiously, it does allow it if Z itself is generic, despite the fact that you have no way to qualify it.

    – RealSkeptic
    15 hours ago













    "oversight" - You're here to pick a fight with the JLS designers, right? :)

    – Lukas Eder
    15 hours ago





    "oversight" - You're here to pick a fight with the JLS designers, right? :)

    – Lukas Eder
    15 hours ago




    2




    2





    Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

    – Rafael Winterhalter
    15 hours ago





    Generics were patched into the language and there are some corner cases such as class literals that were not properly covered. Nothing is perfect, I think there is a chance for an oversight. ;)

    – Rafael Winterhalter
    15 hours ago













    I extended my answer to explain why generification of Z does the trick.

    – Rafael Winterhalter
    15 hours ago





    I extended my answer to explain why generification of Z does the trick.

    – Rafael Winterhalter
    15 hours ago













    @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

    – Andy Turner
    14 hours ago







    @LukasEder I think oversight is right. It's sort of like this question: these cases could have been designed around, but they haven't been; I make no judgment as to whether they should have been.

    – Andy Turner
    14 hours ago















    14














    A possible workaround is to use reflection:



    import java.util.Arrays;

    public class X<T> {
    void m() {
    class Z {}

    for (Object o : Arrays.asList(1, 2, 3))
    if (Z.class.isInstance(o)) {}
    }
    }





    share|improve this answer




























      14














      A possible workaround is to use reflection:



      import java.util.Arrays;

      public class X<T> {
      void m() {
      class Z {}

      for (Object o : Arrays.asList(1, 2, 3))
      if (Z.class.isInstance(o)) {}
      }
      }





      share|improve this answer


























        14












        14








        14







        A possible workaround is to use reflection:



        import java.util.Arrays;

        public class X<T> {
        void m() {
        class Z {}

        for (Object o : Arrays.asList(1, 2, 3))
        if (Z.class.isInstance(o)) {}
        }
        }





        share|improve this answer













        A possible workaround is to use reflection:



        import java.util.Arrays;

        public class X<T> {
        void m() {
        class Z {}

        for (Object o : Arrays.asList(1, 2, 3))
        if (Z.class.isInstance(o)) {}
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 15 hours ago









        Lukas EderLukas Eder

        137k74448987




        137k74448987























            2














            Apparently, by making Z generic compilation succeeds. I expected that to require <T> as the type parameter, but you just have to make it generic, so anything will do



            import java.util.Arrays;

            public class X<T> {
            void m() {
            class Z<Anything> {}

            for (Object o : Arrays.asList(1, 2, 3))
            if (Z.class.isInstance(o)) {}
            }
            }


            Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.






            share|improve this answer





















            • 2





              Once you make Z generic, you don't need to apply the reflection workaround anymore...

              – Lukas Eder
              15 hours ago
















            2














            Apparently, by making Z generic compilation succeeds. I expected that to require <T> as the type parameter, but you just have to make it generic, so anything will do



            import java.util.Arrays;

            public class X<T> {
            void m() {
            class Z<Anything> {}

            for (Object o : Arrays.asList(1, 2, 3))
            if (Z.class.isInstance(o)) {}
            }
            }


            Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.






            share|improve this answer





















            • 2





              Once you make Z generic, you don't need to apply the reflection workaround anymore...

              – Lukas Eder
              15 hours ago














            2












            2








            2







            Apparently, by making Z generic compilation succeeds. I expected that to require <T> as the type parameter, but you just have to make it generic, so anything will do



            import java.util.Arrays;

            public class X<T> {
            void m() {
            class Z<Anything> {}

            for (Object o : Arrays.asList(1, 2, 3))
            if (Z.class.isInstance(o)) {}
            }
            }


            Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.






            share|improve this answer















            Apparently, by making Z generic compilation succeeds. I expected that to require <T> as the type parameter, but you just have to make it generic, so anything will do



            import java.util.Arrays;

            public class X<T> {
            void m() {
            class Z<Anything> {}

            for (Object o : Arrays.asList(1, 2, 3))
            if (Z.class.isInstance(o)) {}
            }
            }


            Proper solution would be qualify the local class, but I don't think you can. Either you refactor it to a private static class or that's probably the best you can get.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 15 hours ago

























            answered 15 hours ago









            Edoardo VacchiEdoardo Vacchi

            759715




            759715








            • 2





              Once you make Z generic, you don't need to apply the reflection workaround anymore...

              – Lukas Eder
              15 hours ago














            • 2





              Once you make Z generic, you don't need to apply the reflection workaround anymore...

              – Lukas Eder
              15 hours ago








            2




            2





            Once you make Z generic, you don't need to apply the reflection workaround anymore...

            – Lukas Eder
            15 hours ago





            Once you make Z generic, you don't need to apply the reflection workaround anymore...

            – Lukas Eder
            15 hours ago











            0














            This should work either. Using reflection too. But seems a valid solution.



            import java.util.Arrays;

            public class X<T> {


            void m() {

            class Z2 {
            }

            for(Object o: Arrays.asList(1,2,3)) {
            if(Z2.class.isAssignableFrom(o.getClass())) {

            }
            }

            }

            }





            share|improve this answer


























            • I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

              – Lukas Eder
              15 hours ago
















            0














            This should work either. Using reflection too. But seems a valid solution.



            import java.util.Arrays;

            public class X<T> {


            void m() {

            class Z2 {
            }

            for(Object o: Arrays.asList(1,2,3)) {
            if(Z2.class.isAssignableFrom(o.getClass())) {

            }
            }

            }

            }





            share|improve this answer


























            • I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

              – Lukas Eder
              15 hours ago














            0












            0








            0







            This should work either. Using reflection too. But seems a valid solution.



            import java.util.Arrays;

            public class X<T> {


            void m() {

            class Z2 {
            }

            for(Object o: Arrays.asList(1,2,3)) {
            if(Z2.class.isAssignableFrom(o.getClass())) {

            }
            }

            }

            }





            share|improve this answer















            This should work either. Using reflection too. But seems a valid solution.



            import java.util.Arrays;

            public class X<T> {


            void m() {

            class Z2 {
            }

            for(Object o: Arrays.asList(1,2,3)) {
            if(Z2.class.isAssignableFrom(o.getClass())) {

            }
            }

            }

            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 15 hours ago









            Mark Rotteveel

            62.2k1479123




            62.2k1479123










            answered 15 hours ago









            JWThewesJWThewes

            191




            191













            • I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

              – Lukas Eder
              15 hours ago



















            • I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

              – Lukas Eder
              15 hours ago

















            I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

            – Lukas Eder
            15 hours ago





            I didn't downvote, but I guess it's because there's a slightly better way to use reflection here, as I've mentioned in my own answer

            – Lukas Eder
            15 hours ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55703849%2ferror-illegal-generic-type-for-instanceof-when-using-local-classes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bruad Bilen | Luke uk diar | NawigatsjuunCommonskategorii: BruadCommonskategorii: RunstükenWikiquote: Bruad

            What is the offset in a seaplane's hull?

            Slayer Innehåll Historia | Stil, komposition och lyrik | Bandets betydelse och framgångar | Sidoprojekt och samarbeten | Kontroverser | Medlemmar | Utmärkelser och nomineringar | Turnéer och festivaler | Diskografi | Referenser | Externa länkar | Navigeringsmenywww.slayer.net”Metal Massacre vol. 1””Metal Massacre vol. 3””Metal Massacre Volume III””Show No Mercy””Haunting the Chapel””Live Undead””Hell Awaits””Reign in Blood””Reign in Blood””Gold & Platinum – Reign in Blood””Golden Gods Awards Winners”originalet”Kerrang! Hall Of Fame””Slayer Looks Back On 37-Year Career In New Video Series: Part Two””South of Heaven””Gold & Platinum – South of Heaven””Seasons in the Abyss””Gold & Platinum - Seasons in the Abyss””Divine Intervention””Divine Intervention - Release group by Slayer””Gold & Platinum - Divine Intervention””Live Intrusion””Undisputed Attitude””Abolish Government/Superficial Love””Release “Slatanic Slaughter: A Tribute to Slayer” by Various Artists””Diabolus in Musica””Soundtrack to the Apocalypse””God Hates Us All””Systematic - Relationships””War at the Warfield””Gold & Platinum - War at the Warfield””Soundtrack to the Apocalypse””Gold & Platinum - Still Reigning””Metallica, Slayer, Iron Mauden Among Winners At Metal Hammer Awards””Eternal Pyre””Eternal Pyre - Slayer release group””Eternal Pyre””Metal Storm Awards 2006””Kerrang! Hall Of Fame””Slayer Wins 'Best Metal' Grammy Award””Slayer Guitarist Jeff Hanneman Dies””Bullet-For My Valentine booed at Metal Hammer Golden Gods Awards””Unholy Aliance””The End Of Slayer?””Slayer: We Could Thrash Out Two More Albums If We're Fast Enough...””'The Unholy Alliance: Chapter III' UK Dates Added”originalet”Megadeth And Slayer To Co-Headline 'Canadian Carnage' Trek”originalet”World Painted Blood””Release “World Painted Blood” by Slayer””Metallica Heading To Cinemas””Slayer, Megadeth To Join Forces For 'European Carnage' Tour - Dec. 18, 2010”originalet”Slayer's Hanneman Contracts Acute Infection; Band To Bring In Guest Guitarist””Cannibal Corpse's Pat O'Brien Will Step In As Slayer's Guest Guitarist”originalet”Slayer’s Jeff Hanneman Dead at 49””Dave Lombardo Says He Made Only $67,000 In 2011 While Touring With Slayer””Slayer: We Do Not Agree With Dave Lombardo's Substance Or Timeline Of Events””Slayer Welcomes Drummer Paul Bostaph Back To The Fold””Slayer Hope to Unveil Never-Before-Heard Jeff Hanneman Material on Next Album””Slayer Debut New Song 'Implode' During Surprise Golden Gods Appearance””Release group Repentless by Slayer””Repentless - Slayer - Credits””Slayer””Metal Storm Awards 2015””Slayer - to release comic book "Repentless #1"””Slayer To Release 'Repentless' 6.66" Vinyl Box Set””BREAKING NEWS: Slayer Announce Farewell Tour””Slayer Recruit Lamb of God, Anthrax, Behemoth + Testament for Final Tour””Slayer lägger ner efter 37 år””Slayer Announces Second North American Leg Of 'Final' Tour””Final World Tour””Slayer Announces Final European Tour With Lamb of God, Anthrax And Obituary””Slayer To Tour Europe With Lamb of God, Anthrax And Obituary””Slayer To Play 'Last French Show Ever' At Next Year's Hellfst””Slayer's Final World Tour Will Extend Into 2019””Death Angel's Rob Cavestany On Slayer's 'Farewell' Tour: 'Some Of Us Could See This Coming'””Testament Has No Plans To Retire Anytime Soon, Says Chuck Billy””Anthrax's Scott Ian On Slayer's 'Farewell' Tour Plans: 'I Was Surprised And I Wasn't Surprised'””Slayer””Slayer's Morbid Schlock””Review/Rock; For Slayer, the Mania Is the Message””Slayer - Biography””Slayer - Reign In Blood”originalet”Dave Lombardo””An exclusive oral history of Slayer”originalet”Exclusive! Interview With Slayer Guitarist Jeff Hanneman”originalet”Thinking Out Loud: Slayer's Kerry King on hair metal, Satan and being polite””Slayer Lyrics””Slayer - Biography””Most influential artists for extreme metal music””Slayer - Reign in Blood””Slayer guitarist Jeff Hanneman dies aged 49””Slatanic Slaughter: A Tribute to Slayer””Gateway to Hell: A Tribute to Slayer””Covered In Blood””Slayer: The Origins of Thrash in San Francisco, CA.””Why They Rule - #6 Slayer”originalet”Guitar World's 100 Greatest Heavy Metal Guitarists Of All Time”originalet”The fans have spoken: Slayer comes out on top in readers' polls”originalet”Tribute to Jeff Hanneman (1964-2013)””Lamb Of God Frontman: We Sound Like A Slayer Rip-Off””BEHEMOTH Frontman Pays Tribute To SLAYER's JEFF HANNEMAN””Slayer, Hatebreed Doing Double Duty On This Year's Ozzfest””System of a Down””Lacuna Coil’s Andrea Ferro Talks Influences, Skateboarding, Band Origins + More””Slayer - Reign in Blood””Into The Lungs of Hell””Slayer rules - en utställning om fans””Slayer and Their Fans Slashed Through a No-Holds-Barred Night at Gas Monkey””Home””Slayer””Gold & Platinum - The Big 4 Live from Sofia, Bulgaria””Exclusive! Interview With Slayer Guitarist Kerry King””2008-02-23: Wiltern, Los Angeles, CA, USA””Slayer's Kerry King To Perform With Megadeth Tonight! - Oct. 21, 2010”originalet”Dave Lombardo - Biography”Slayer Case DismissedArkiveradUltimate Classic Rock: Slayer guitarist Jeff Hanneman dead at 49.”Slayer: "We could never do any thing like Some Kind Of Monster..."””Cannibal Corpse'S Pat O'Brien Will Step In As Slayer'S Guest Guitarist | The Official Slayer Site”originalet”Slayer Wins 'Best Metal' Grammy Award””Slayer Guitarist Jeff Hanneman Dies””Kerrang! Awards 2006 Blog: Kerrang! Hall Of Fame””Kerrang! Awards 2013: Kerrang! Legend”originalet”Metallica, Slayer, Iron Maien Among Winners At Metal Hammer Awards””Metal Hammer Golden Gods Awards””Bullet For My Valentine Booed At Metal Hammer Golden Gods Awards””Metal Storm Awards 2006””Metal Storm Awards 2015””Slayer's Concert History””Slayer - Relationships””Slayer - Releases”Slayers officiella webbplatsSlayer på MusicBrainzOfficiell webbplatsSlayerSlayerr1373445760000 0001 1540 47353068615-5086262726cb13906545x(data)6033143kn20030215029