0

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 ?

1
  • 1
    This doesn't only apply with inheritance. You can shadow an instance variable or a static variable of your class, with a local variable in the way you have done here. This is not recommended practice. Commented Nov 24, 2013 at 10:37

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

4 Comments

oh I see, now it makes sense
@Bohemian I rolled back because MyFace.a isn't FQ (at least not unless it's packageless). I don't know what the official term for the class-qualified name is, and I'm not up for JLS archaeology tonight.
"by qualifying it with the class name" would fit.
np, but "longer" isn't the right word though. Maybe just "qualified" instead of "fully qualified"
0

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

1 Comment

thanks, how can I see the complied assembly codes as you put here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.