Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Instanceof check for final class

Hi guys!

Could you explain why such a code brings compilation error:


public class A{
    public static  void main(String[] args) {
        boolean b = new C() instanceof I;
    }
}

interface I {}

final class C {}


and with non-final class C everything is OK. Thanks!

Hi Dmytro!

You can use any non-final class as the left-side-operand operand for the instanceof operator, if the right-side operand is an interface (can use any interface). That explanation will be available to you after your tutor reviews your test.

Hi Dmytro!


Kiraukr is right, you can do that at compile time, even though improper use will end up throwing a ClassCastException at run time.


For a little bit better explanation, please take a look at this post.

ExamLab © 2008 - 2024