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 (3: bipush 30)
If local variable a is defined local variable 1 local1 is pushed then multiply operation is done.
5: iload_1
6: iconst_2
7: imul
if you want to use both of them you have to Usage Class name where static variable is defined.
System.out.println(a*2); // will print 6
System.out.println(MyFace.a); //will print 15
Here is a the example code local variable a is defined
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."<init>":()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
}
*********************************************
Here is a the example code static variable a is defined
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."<init>":()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
}
to print byte code from compiled class file use javap tool under $JDK_HOME/bin