Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may refer following section / examples in <a href="http://docs.oracle.com/javase/specs/jls/se7/html/index.html" rel="noreferrer">Java language specification</a> that explains about the topic. </p> <ol> <li>Example 8.3.1.1-3. <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#d5e10238" rel="noreferrer">Hiding of Instance Variables</a></li> <li>Section 8.4.8. <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8" rel="noreferrer">Inheritance, Overriding, and Hiding</a> and related examples</li> </ol> <p>Rest of my post is an additional information for those who are interested in scratching the surface of jvm internals on this subject. We can start by examining the byte codes generated for class A using javap. Following disassembles the byte codes into a human readable text based instructions (mnemonics).</p> <pre><code>javap -c A.class </code></pre> <p>Without getting lost in many details of the entire dis-assembly, we can focus on the lines corresponding to b.print and b.a</p> <pre><code>9: invokevirtual #4 // Method B.print:()V ... ... 16: getfield #6 // Field B.a:I </code></pre> <p>We can immediately infer that the op codes used for accessing the method and a variable are different. If you are from a C++ school, you could sense that all method calls are virtual by default in java.</p> <p>Now let us write another class A1 identical to A, but just has a casting for accessing variable 'a' in C. </p> <blockquote> <p>public class A1 {<br> &nbsp;&nbsp;public static void main(String[] args) {<br> &nbsp;&nbsp;&nbsp;&nbsp;B b=new C();<br> &nbsp;&nbsp;&nbsp;&nbsp;b.print(); //casting is irrelevant here because methods are anyway bound at runtime &nbsp;&nbsp;&nbsp;&nbsp;System.out.println(((C)b).a);// the casting allows us to access of value of a in C<br> &nbsp;&nbsp;}<br> } <br/></p> </blockquote> <p>Compile the file and disassemble the class. </p> <blockquote> <p>javap -c A1.class </p> </blockquote> <p>You would notice that dis-assembly now points to C.a instead of B.a </p> <blockquote> <p>19: getfield #6 // Field C.a:I</p> </blockquote> <p>if you want to dig deep into this, here goes additional information:<br> - invokevirtual corresponds to opcode 0xb6<br> - getfield corresponds to opcode 0xb4</p> <p>You can find a JVM specification that explains comprehensively about these opcodes at - <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html" rel="noreferrer">http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html</a><br> Check out in amazon.com for "Java Virtual Machine" books that could make life little more easier for decoding the specification.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload