So, I get what it does in functionality, but I can't seem to understand how it gets the right answer here.
So here is how I'm understanding this code:
applePrice = 0.
apple.addprice increases applePrice to 50.
apple2.addPrice increases it by 100 to 150.
It seems to me like the math should already be done, but of course if you just run the code as is without adding anything to it, the output is 0. Additionally, just adding Apple.applePrice = appleprice returns an output of 100.
Apple.applePrice += appleprice returns 150.
If I'm understanding this correctly, this means that Apple.applePrice has a value of 50 at this point. Why is this? I would have expected it to be either 0 or 150.
package en.codegym.task.jdk13.task04.task0402;
/*
Price of apples
*/
public class Solution {
public static void main(String[] args) {
Apple apple = new Apple();
apple.addPrice(50);
Apple apple2 = new Apple();
apple2.addPrice(100);
System.out.println("Apple price " + Apple.applePrice);
}
public static class Apple {
public static int applePrice = 0;
public static void addPrice(int applePrice) {
}
}
}