I don't understand what I don't understand, so I can't explain it well, but if you can tell me what I can't tell you with a supplementary request, it will be very helpful. Thank you in advance.

A problem with Java silver SE8 array instances and element values.
You can choose the execution result of the next program from the choices.

public class Item {
    String name;
    int price = 100;
public class Main {
    public static void main (String [] args) {
        Item [] items = new Item [3];
        int total = 0;
        for (int i = 0;i<items.length;i ++) {
            total + = items [i] .price;
        System.out.println (total);

The correct answer is "An exception is thrown at run time", and part of the commentary is "It's easy to get confused by beginners. Is mentioned. "

I understand that

objects can usetotal + = object name.price;to assign the price of each object to total. + = Does it mean that the array variable name [] .price;assigns the price of each element of the array to total? That would seem to be an exception in any case, but please give me an example of a program that uses a statement like this (array variable name [].).

  • Answer # 1

    Item [] items = new Item [3];// {null, null, null}

    As you can see if you debug, at this point you have just three boxes to put theItemclass, and the contents of each box are empty.
    (Primitive types such as int contain initial values.)

    In other words, every element of the array is null until you assign an instance to each element.

    for (int i = 0;i

  • Answer # 2

      I understand that the price of each object can be substituted into total with total + = object name.price;if it is an

    object, but total + = array variable name [] .price;Does that mean that the price for each element of the array is assigned to total?

    In this case, array elements are also objects.
    It's the same as writing:

    Item item = items [i];
    total + = item.price;