Ramya Subraamanian wrote:so shouldn't this be like below. or am i missing something here.
I think you are missing something here. Or maybe I don't fully understand your question/doubt.
The study guide tries to explain here that the methods
ofYears() and
ofWeeks() are class (static) factory methods. So if you want to create a period of 1 year, you write this code
But as you know you can invoke a class (static) method on an instance as well, but this method is completely unaware of the instance. So this line is valid
Java syntax
But you might think you have created a period of 1 year and 1 week, but that's
incorrect! You have created a period of 1 week (7 days). And printing both periods will prove this
And that's exactly what the book tries to explain here. It's a warning for a potential trick question on the actual exam. f you see the following statement in a code snippet
you might think you have created a period of 1 year and 1 week (and thus you might select an incorrect answer), but in fact you have created a period of 1 week instead. If you want to create a period of 1 year and 1 week, you'll need to use something like
And if you add this to a
LocalDate instance, you'll get the expected output
Hope it helps!
Kind regards,
Roel