• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working with date and time (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In page 147 of the book.

only ofWeeks(1) is added to Period as specified in the errata and the book. and ofYears(1) is not added to Period, and is given in the book . so shouldn't this be like below. or am i missing something here.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 codeBut 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 syntaxBut 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 snippetyou 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 likeAnd if you add this to a LocalDate instance, you'll get the expected output

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, Thank you Roel. I understand now.
It doesnt matter whether it is



or it is



only the period of 1 week is created and added when we use in our LocalDate instance. thats because ofYears() and ofWeeks() are static methods.



gives us the desired result because withDays is not static.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed! It's just a heads-up of the authors, so you are not fooled on the exam. The Date/Time API uses method chaining a lot. And although considered a bad practice, you can invoke class (static) methods on an instance. So it's very important to know which methods are instance methods and which ones are class (static) methods, because the outcome will be completely different.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic