About Question enthuware.ocajp.i.v8.3.1486 :

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

Moderator: admin

Post Reply
baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

About Question enthuware.ocajp.i.v8.3.1486 :

Post by baichen7788 »

Is that explanation trying to say null.url is valid?

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

Re: About Question enthuware.ocajp.i.v8.3.1486 :

Post by admin »

Not really. Please read the complete explanation carefully:
Note the method signature. It returns a reference to an object of class DatabaseWrapper. Thus, getDatabase().url means we are accessing url field of the object returned by the method. Now, since the class of the object returned by the method is DatabaseWrapper and the field url is a static field of the class, the compiler creates the instruction for the JVM to access this field directly using the class reference instead of the object reference returned by the method at runtime. Thus, the JVM does not need to depend on the actual object returned by the method at run time to access url. So even if the method returns null at run time, it doesn't matter because the JVM doesn't even access the reference returned by the method.
What it is trying to say is that even if the reference resolves to null at runtime, it doesn't matter because the JVM doesn't need the object reference to access static members anyway. The compiler replaces getDatabase().url with DatabaseWrapper.url!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests