What happens when base and derived classes each have variables with the same name

后端 未结 4 648
暗喜
暗喜 2020-12-02 16:14

Consider the int a variables in these classes:

class Foo {
    public int a = 3;
    public void addFive() { a += 5; System.out.print(\"f \"); }         


        
4条回答
  •  眼角桃花
    2020-12-02 16:37

    Here F is of type Foo and f variable is holding Bar object but java runtime gets the f.a from the class Foo.This is because in Java variable names are resolved using the reference type and not the object which it is referring.

提交回复
热议问题