Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just look at bytecode of two different usages (with local variable a and without it). As you may see that If only static a is defined compiler will directly use 30 <code>(3: bipush 30)</code> If local variable a is defined local variable 1 local1 is pushed then multiply operation is done. </p> <pre><code> 5: iload_1 6: iconst_2 7: imul </code></pre> <p>if you want to use both of them you have to Usage Class name where static variable is defined.</p> <pre><code>System.out.println(a*2); // will print 6 System.out.println(MyFace.a); //will print 15 </code></pre> <p><strong>Here is a the example code local variable a is defined</strong></p> <pre><code>public class XFace implements MyFace { @Override public void smile() { int a=3; System.out.println(a*2); // will print 6 } } Compiled from "XFace.java" public class XFace extends java.lang.Object implements MyFace{ public XFace(); Code: 0: aload_0 1: invokespecial #10; //Method java/lang/Object."&lt;init&gt;":()V 4: return public void smile(); Code: 0: iconst_3 1: istore_1 2: getstatic #17; //Field java/lang/System.out:Ljava/io/PrintStream; 5: iload_1 6: iconst_2 7: imul 8: invokevirtual #23; //Method java/io/PrintStream.println:(I)V 11: return } ********************************************* </code></pre> <p><strong>Here is a the example code static variable a is defined</strong></p> <pre><code>public class XFace implements MyFace { @Override public void smile() { //int a=3; System.out.println(a*2); // will print 30 } } Compiled from "XFace.java" public class XFace extends java.lang.Object implements MyFace{ public XFace(); Code: 0: aload_0 1: invokespecial #10; //Method java/lang/Object."&lt;init&gt;":()V 4: return public void smile(); Code: 0: getstatic #17; //Field java/lang/System.out:Ljava/io/PrintStream; 3: bipush 30 5: invokevirtual #23; //Method java/io/PrintStream.println:(I)V 8: return } </code></pre> <p>to print byte code from compiled class file use javap tool under $JDK_HOME/bin</p>
 

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