Question

I know static means, there is only one instance of it in the memory. I know final means it can not be changed or subclassed,I also know any variable defined in a java interface is static final

so now here is the question , why can I over ride final static variable "a" in the interface "MyFace" in the "XFace" class?

Example:

public interface MyFace {
    static final int a = 15;

    void smile();
}

then here in the class I can easily over ride the a, with a local a,

public class XFace implements MyFace {

    @Override
    public void smile() {
        int a=3;  // over riding interface's a variable and suprsingly it works !
        System.out.println(a*2);  // will print 6
    }

why can I define int a=3 in smile() method ? isn't "a" final and static ? how could it be overridden ?

Was it helpful?

Solution

It's not overridden, it's shadowed, meaning that there's a closer variable with the same simple name that takes precedence. You can still use the static final by using its longer name, MyFace.a.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top