About Question enthuware.ocpjp.v17.2.3721 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Val Martinez
Posts: 11
Joined: Thu Sep 01, 2022 10:58 am
Contact:

About Question enthuware.ocpjp.v17.2.3721 :

Post by Val Martinez »

As for me, the first option marked as wrong,

Code: Select all

if (a instanceof A a1) a1.a();
does work perfectly in a real enviroment with JDK17

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3721 :

Post by admin »

No, it doesn't. Please make sure you have copied the code correctly.
If you like our products and services, please help us by posting your review here.

Badem48
Posts: 26
Joined: Thu Aug 24, 2023 4:33 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3721 :

Post by Badem48 »

I do not know if it is just me but this code, which is first option works on my machine?

Code: Select all

public class TestClass {
    public static void main(String[] args) {
        B b = new C();
        A a = b;
        if (a instanceof A a1) a1.a();

    }
}

class A {
    void a(){ System.out.println("a"); }
}
class B extends A {
    void b(){ System.out.println("b"); }
}
class C extends B {
    void c(){ System.out.println("c"); }
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3721 :

Post by admin »

It doesn't compile on Java 17 but compiles on Java 21.
This is due to a change in the Java language specification for instanceof.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 36 guests