I solved the problem but am puzzling over something. The given code calls the addPrice() method twice, once on apple instance: apple.addPrice(50);, and once on apple2 instance: apple2.addPrice(100);.
I am uncertain what exactly is going on here with the dot operator being used to call a static method on an instance of a class, as opposed to a Class itself, for example: Apple.addPrice();. If you substitute class name Apple in, instead of apple and apple2, the code works, and it seems clear to me why that does work--it is simply calling the addPrice method from Apple class.
But I am not clear on exactly what is happening in the case of <object> dot < static method name>....the lecture did not cover this particular case.
apple.addPrice() vs. Apple.addPrice?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
12 January 2019, 20:33
Static class properties are shared between all objects of that class (or inherited classes), and can be called by individual objects or the class itself.
+2
qsasdgkl
7 April 2019, 13:30
Not the original poster but thanks anyway!
This is the important part of information that I had missed! Now it all make sense!
+1