I've figured out the answer by hints here, but want to know why 'this' doesn't work?
Why this.applesPrice doesn't work here?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Okanlawon Oluwatobi Damilare
15 March 2019, 16:27
Apple.applePrice
0
Niranjan Soni
22 September 2018, 20:47
In simple terms ClassName.variableName is used to access static variables.
While this.variableName is used in case to access non-static variables.
+5
Sameen
25 August 2018, 11:37
As you can see that the addPrice method is static. Which means that the method isn't associated to any particular object. Whereas, 'this.variableName' is non-static variable. Thus, as also specified in the lesson, we can't access non-static variables through static methods. because the method does not know to which object of the class are we referring when we say 'this'.
Hope it helps.
+4