Hi!
I have a superclass called product and 3 subclasses called Laptop, Web and Microphone. In each subclass a created a static variable that counts the number of products from each class. I created an Array list that shows the products from each but my problem is that a don't know how to call the static variable assigned to each object.
What I have tried:
The page of subclass laptop:
public class laptop extends Products {
static int stock=0;
public laptop(String name, String color, Double price, Double discount) {
super(name, color, price, discount);
stock++;
public static int getStock() {
return stock;
}
}
The class of the Array List that creates the objects:
<pre>ArrayList <Products> catalog = new ArrayList<>();
catalog.add( new web("web", "red", 145.0, 1.0));
catalog.add( new microphone("microphone", "red", 145.0, 1.0));
catalog.add( new laptop("laptop", "red", 145.0, 1.0));
So, If I would call web.getStock everything works fine and I can access the stock. But if I want to display the list using a for loop I don't really know how to access the stock of each product.
<pre> for(int i=0; i<catalog.size(); i++){
System.out.println(catalog.get(i).name + " " + (Here! I don't know how to acces the static variable of each catalog.get(i).name);