About Question enthuware.ocajp.i.v8.3.1486 :
Moderator: admin
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
About Question enthuware.ocajp.i.v8.3.1486 :
Is that explanation trying to say null.url is valid?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.3.1486 :
Not really. Please read the complete explanation carefully:
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!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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Apr 25, 2022 3:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.3.1486 :
If this is the case why it is the getDatabase() method getting evaluated and prints "Getting Db..."?The compiler replaces getDatabase().url with DatabaseWrapper.url!
If getDatabase() is indeed getting evaluated, why is the result of its execution not getting honored?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.3.1486 :
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:
If you make url an instance field, you will see the following output of the disassembler:
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 disassemble the generated byte code for DatabaseWrapper.class ( use command javap -c DatabaseWrapper ), you will see the following:
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.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;
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.
Who is online
Users browsing this forum: No registered users and 64 guests