• Post Reply Bookmark Topic Watch Topic
  • New Topic

methods can run before class initialization?  RSS feed

 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Near the bottom of this page discussing assertions, it states that "few programmers are aware of the fact that a class's constructors and methods can run prior to its initialization." I guess I was not one of the programmers who is aware of that :roll:

Could anyone explain under what conditions this might occur?

Thanks
Geoffrey
[ March 30, 2005: Message edited by: Geoffrey Falk ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, running this class prints "0", showing that the constructor and the method "eek()" did, indeed, run before class initialization was complete (x gets set to 37 during that process.)

 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ernest,

I tried your code and I found that you are correct, it prints 0. However, the statement on the web page about assertions does not seem to be correct.



When assertion status is disabled, this code prints "false". But the web page states that it would display true if the method ran before initialization.

Geoffrey
[ March 30, 2005: Message edited by: Geoffrey Falk ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!