Home » Java » Shouldnt this class give an “infinite” amount of objects by having an array of instatiated objects of the same class? Why does it work?

Shouldnt this class give an “infinite” amount of objects by having an array of instatiated objects of the same class? Why does it work?

Posted by: admin July 12, 2018 Leave a comment

Questions:
public class Drink {

    private String name;
    private String description;

    public static final Drink[] drinks = {
        new Drink("Latte", "A couple of espresso shots with steamed milk"),
        new Drink("Cappuccino", "Espresso, hot milk, and a steamed milk foam"),
        new Drink("Filter","Highest quality beans roasted and brewed fresh")
    };
}

Once I type something like Drink drink = Drink.drinks[0] why
doesn’t the program continuously make new drinks since the new drink object?
Now has an array of new Drink objects and each one of those has a new
array of drink objects so on a so forth?

Answers: