为什么我必须在构造函数内使用 “this” 关键字来初始化实例变量?

huangapple go评论61阅读模式
英文:

Why I must use a this keyword to initialize an instance variable inside constructor?

问题

我对JAVA非常新并且正在尝试使用以下代码通过构造函数初始化对象变量的值

public class Javaprog {
    int rollnumber;
    String name;

    Javaprog(int rollnumber, String name) {
        rollnumber = rollnumber;
        name = name;
    }

    public static void main(String[] args) {
        Javaprog student1 = new Javaprog(12, "Simon");
        System.out.println(student1.rollnumber);
        System.out.println(student1.name);
    }
}

我想要理解为什么上面的代码返回rollnumber和name的默认值0和null),除非我在构造函数内部使用"this"来引用变量就像下面这样

this.rollnumber = rollnumber;
this.name = name;

我知道"this"指的是当前对象但我的观点是当构造函数在创建对象时运行时难道它默认不会理解这些变量与正在创建的对象相关吗

是不是没有使用this关键字时它们只是"类变量"并且不会连接到正在创建的对象上

在这里找到了一个类似的问题但没有完全理解使用this的原因
https://stackoverflow.com/questions/39707976/java-this-keyword-inside-constructor
英文:

I'm very new to JAVA and trying the following code to initialize the values of object variable using constructor:

public class Javaprog {
int rollnumber;
String name;

Javaprog(int rollnumber, String name) {
rollnumber = rollnumber;
name = name;
}

public static void main(String[] args) {
Javaprog student1 = new Javaprog(12, "Simon");
System.out.println(student1.rollnumber);
System.out.println(student1.name);
        }
}

I want to understand why the code above returns the default values of rollnumber and name(0 and null), unless I use "this" to reference the variables inside the constructor like below:

this.rollnumber = rollnumber;
this.name = name;
I'm aware this refers to the current object, but my point is when the constructor runs for creation of an object, does it not understand by default that these variables relate to the object being created .

Is it that, without the use of this keyword they are just "class variables" and don't attach to the object being created.

Found a similar Q here, but did not fully understand the mandate to use this:
https://stackoverflow.com/questions/39707976/java-this-keyword-inside-constructor

答案1

得分: 0

让我们暂时将构造函数放在一边,仅从代码角度来看待它。

参数rollnumber和name是局部变量,它们的作用域仅限于函数内部。因此,当你说:

rollnumber = rollnumber;

它只是将局部变量rollnumber的当前值赋值给它本身(什么也没做)。无法区分rollnumber(函数内的参数/局部变量)和实例变量rollnumber。

为了确保编译器理解我们的意图,我们使用:

this.rollnumber(指的是实例变量)= rollnumber(参数);

为了避免这种情况,你可以给实例变量取一个不同的名字,比如rollnum。这样,编译器会首先在局部作用域内(即在构造函数内部)搜索rollnum(未找到),然后在更高的作用域内搜索,这里它会作为实例变量找到并正确赋值。

rollnum = rollnumber;

会起作用。

英文:

let’s keep constructors aside and just look at it from a code point of view.

the parameters rollnumber and name are local variables, their scope is only in the function. So when you say

rollnumber = rollnumber;

It simply assigns the current value of local variable rollnumber to itself (does nothing). There is no way to differentiate rollnumber (the parameter/loca variable inside the function) and the instance variable, rollnumber.

To make sure the compiler understands what we want, we use

this.rollnumber (referring to instance variable) = rollnumber (parameter);

To avoid this, you can name your instance variable something else, like rollnum. This way the compiler will search for rollnum in the local scope (meaning within the constructor function, not found) then in the higher scope, where it will be found as an instance variable and correctly assigned.

 rollnum = rollnumber;

will work.

huangapple
  • 本文由 发表于 2020年8月20日 03:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63493990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定