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?

Online
admin
Site Admin
Posts: 10036
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.

mehulgupta
Posts: 1
Joined: Mon Apr 25, 2022 3:16 pm
Contact:

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

Post by mehulgupta »

The compiler replaces getDatabase().url with DatabaseWrapper.url!
If this is the case why it is the getDatabase() method getting evaluated and prints "Getting Db..."?
If getDatabase() is indeed getting evaluated, why is the result of its execution not getting honored?

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

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

Post by admin »

You are misunderstanding the issue here. The point is that access to the url field is not done using the return value of getDatabase. The replacement being talked about above is about accessing the url field. getDatabase method invocation is a separate thing.

If you disassemble the generated byte code for DatabaseWrapper.class ( use command javap -c DatabaseWrapper ), you will see the following:
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: invokestatic #21 // Method getDatabase:()LDatabaseWrapper;
6: pop
7: getstatic #27 // Field url:Ljava/lang/String;
Here, you can see that return value of invokestatic at 3: is popped off the stack at 6. It is not used for accessing the url field. The url field is accessed independently.

If you make url an instance field, you will see the following output of the disassembler:
0: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream;
3: invokestatic #29 // Method getDatabase:()LDatabaseWrapper;
6: getfield #9 // Field url:Ljava/lang/String;

You can see here that the result of the method is not popped off. It is used to access the url field at 6.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: admin, Google [Bot] and 53 guests