According to the lesson, static method can not access non-static variables. However, addPrice is a static method and applePrice variable of Apple class is also static. Why then we needed to use Apple.applePrice to access the variable in addPrice method?
Why Apple.addPrice needed in addPrice method?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Aashita Saxena
19 July 2020, 01:17
That was helpful. Thanks!
0
Gellert Varga
18 July 2020, 00:37
- in this line, the 'applePrice' is a local variable. It is just a passed argument, just a passed value.
It is not the same as the 'Apple.applePrice'!
(We even could name it to anything else, for example 'inflationaryPrice', etc.)
applePrice: just the names are the same, but they are two different variables.
A differentiation must be made between them.
Apple.applePrice == the static variable of the Apple class.
The local variable 'applePrice' covers the other 'applePrice' variable, so You need to write the full name of the other 'applePrice' (with its classname.)
+3