Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Period Class,Chain methods in declaration .  RSS feed

 
Gheyath Nasani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can not I make Chain method in declaration for class "Period" ?

and what the problem when  "Accessing static method "?

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, we don't exactly know what your Period class looks like, nor do we know the exact error message you are getting... perhaps it would be good to give us a bit more context?

Henry
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Period mean this? Please supply the exact error message. Is that an error or a warning? The code compiled without errors for me.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright!  I didn't dump everything after taking the OCA: I remembered this:

Sybex OCA8, pp.146-147 wrote:There are fve ways to create a Period class:
Period annually = Period.ofYears(1); // every 1 year
Period quarterly = Period.ofMonths(3); // every 3 monthsWorking with Dates and Times 147
Period everyThreeWeeks = Period.ofWeeks(3); // every 3 weeks
Period everyOtherDay = Period.ofDays(2); // every 2 days
Period everyYearAndAWeek = Period.of(1, 0, 7); // every year and 7 days

There’s one catch. You cannot chain methods when creating a Period. The following
code looks like it is equivalent to the everyYearAndAWeek example, but it’s not. Only the
last method is used because the Period.ofXXX methods are static methods.

Period wrong = Period.ofYears(1).ofWeeks(1); // every week
This tricky code is really like writing the following:

Period wrong = Period.ofYears(1);
wrong = Period.ofWeeks(7);
 
Gheyath Nasani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Well, we don't exactly know what your Period class looks like, nor do we know the exact error message you are getting... perhaps it would be good to give us a bit more context?

Henry


I've got warning : "Accessing static method ofWeeks".

I've put my code in this post , and my question about this class https://docs.oracle.com/javase/8/docs/api/java/time/Period.html

 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it is a warning not an error. Are you using Eclipse? You really shou‍ld provide the full details if you want help.
The quote from the Sybex book explains that if you chain method calls with Period, you get the wrong result.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
The quote from the Sybex book explains that if you chain method calls with Period, you get the wrong result.

...and IDEs know this and warn "Accessing static method OfWeeks"

 
Gheyath Nasani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote:
Campbell Ritchie wrote:
The quote from the Sybex book explains that if you chain method calls with Period, you get the wrong result.

...and IDEs know this and warn "Accessing static method OfWeeks"



I am well aware that IDE know this and warn "Accessing static method OfWeeks". But my question about "What is problematic when accessing static method ofWeeks ??  
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gheyath Nasani wrote:
Julian West wrote:
Campbell Ritchie wrote:
The quote from the Sybex book explains that if you chain method calls with Period, you get the wrong result.

...and IDEs know this and warn "Accessing static method OfWeeks"



I am well aware that IDE know this and warn "Accessing static method OfWeeks". But my question about "What is problematic when accessing static method ofWeeks ??  


The Sybex book explains why.  What is still unanswered?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote: . . .
...and IDEs know this and warn "Accessing static method OfWeeks"

No, what happens is that the left call to an ofXXX method (a factory method) returns an instance of Period, and you are then calling the right static method on an instance. Eclipse is programmed to show a warning if you try to call a static member on an instance rather than on the name of the class. It is not that it “knows” anything specific about Period, but that dangerous confusion can arise from calling static methods on instance names.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Julian West wrote: . . .
...and IDEs know this and warn "Accessing static method OfWeeks"

It is not that it “knows” anything specific about Period, but that dangerous confusion can arise from calling static methods on instance names.

I didn't say that specifically.  I only quoted the book that said that you cannot chain Period because you cannot chain static methods and Period is static.

I pointed out that IDEs know this (chaining static methods and that Period is static) and provide a specific warning about the wayward static method (this is exactly what NetBeans does, anyway).  The book makes no mention of IDEs and doesn't take a position on it.  It does, however, say that you can compile this but it won't work.  When you add it all up, the IDE gives a context-sensitive warning about it.  I'm not seeing any confusion, danger, or imprecision here.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The IDE gives a warning because there is a risk of confusion when you call a static method on an instance. There is the possibility that the method from the runtime type of the instance will be different from that actually called because static methods are bound statically and called on the declared type rather than the runtime tpe.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate on Campbell Ritchie's answer, when you execute a static method by using an instance of the class, all of the parameters of the instance are completely ignored. So if you have Period instances var1 and var2, then var1.ofDays(2) will return an instance of Period that has all of its fields set based solely on the value of its argument (2), ignoring any values previously set for that instance. This vat1.ofDays(2) will be equivalent to var2.ofDays(2), regardless of what var1 and var2 look like.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!