Why does Java bind variables at compile time?

前端 未结 4 601
心在旅途
心在旅途 2020-12-04 14:15

Consider the following example code

class MyClass {
    public String var = \"base\";

    public void printVar() {
        System.out.println(var);
    }
}
         


        
4条回答
  •  臣服心动
    2020-12-04 14:53

    In java, this is by design. Because, the set up of fields to be dynamically resolved would make things to run a bit slower. And in real, there's not any reason of doing so. Since, you can make your fields in any class private and access them with methods which are dynamically resolved.

    So, fields are made to resolved better at compile time instead :)

提交回复
热议问题