Understanding on the task :-
The static variable applePrice Line 17 has been accessed by the class name Apple.applePrice at Line no 22
Line 16 Apple Class method addPrice() also static Line 19
2 objects of the Apple class Line 9 apple and Line 11 apple2 which is calling the static addprice () method of Apple Class Line 10 and 12 respectively.
The static addPrice(int applePrice ) method having int applePrice as argument which is local variable for this method it will not return the calculated price to main() method as it is void.
I have called the static variable applePrice using class name as Apple.ApplePrice
assin it the cost of both the apples through object as:-
Apple.applePrice=apple.applePrice+apple2.applePrice;
also tried
Apple.applePrice=this.applePrice+applePrice;
package com.codegym.task.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("The cost of apples is " + Apple.applePrice);
}
public static class Apple {
public static int applePrice = 0;
public static void addPrice(int applePrice) {
//write your code here
Apple.applePrice=apple.applePrice+apple2.applePrice;
}
}