My understanding of the implementation of the body method:

created a reference variable with the name month and type Month , then called the object now() method, i.e. assigned the current month, which is obtained from the system. Then in the condition we compare the month which was obtained by the now() method and the month from enum Month .

Question 1:

How the getMonth() method is implemented, and in general the construction Month month = LocalDate.now().getMonth(); My guess is that when the now() method gets the current date, this date is converted to the enum type, and then the assignment and further execution of the if takes place. Is my understanding of this problem correct?

Question 2:

Month month = LocalDate.now().getMonth(); I know that you can call a method using Object.method() , but like in this example, we have the Object.method().method() construct. How can you call two methods at the same time, and where to read about it.

Thank you for your help.

  static boolean isSummer() { Month month = LocalDate.now().getMonth(); if (month == Month.JUNE || month == Month.JULY || month == Month.AUGUST) { return true; } else { return false; } 

    1 answer 1

    Question 1:

    The LocalDate object stores data in variables:

     /** * The year. */ private final int year; /** * The month-of-year. */ private final short month; /** * The day-of-month. */ private final short day; 

    And accordingly, when you call the now() method, these variables are filled with the appropriate values.

    When the getMonth() method is called, the following code is executed:

     public Month getMonth() { return Month.of(month); } 

    This code returns an enumeration ( enum ) “converting” a value from a variable of type short to enum . All of the above can be easily found by looking at the source codes of the respective classes in java, they are available in jdk.

    Question 2:

    How can you call two methods at the same time, and where can you read about it?

    See objects have methods that can be called through the operator . (точка) . (точка) for example LocalDate.now() . These methods can return objects on which methods can also be called in turn (induction principle). Therefore, you can write LocalDate.now().getMonth() and even continue the call chain by calling any of the methods of the Month object.