• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 182
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 R Subramanian
Ranch Hand
Posts: 182
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic