We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sorry, I don't really see notifications because I get too many. It basically expressly specifies that I'm referring to a field belonging to the object that the running code belongs to. In Java, the convention is to name the constructor parameters the same thing as the object fields you're going to assign them to. For example, this code:
classDemo{// This is the variable that's referenced by this.variableStringvariable;publicDemo(Stringvariable){// Within the scope of this constructor, 'variable' refers to the parameter// and this.variable refers to the object fieldthis.variable="I'm a field in a Demo object.";System.out.println("variable: "+variable);System.out.println("this.variable: "+this.variable);}publicstaticvoidmain(String[]args){Demodemo=newDemo("I'm an argument.");}}
Day 4: Class vs. Instance
You are viewing a single comment's thread. Return to all comments →
Sorry, I don't really see notifications because I get too many. It basically expressly specifies that I'm referring to a field belonging to the object that the running code belongs to. In Java, the convention is to name the constructor parameters the same thing as the object fields you're going to assign them to. For example, this code:
Prints this:
This might make more sense when you do the challenge on variable scope.