• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Assertions

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have been reading the Assertion notes at:
http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html
In the section on Preconditions, the following statements are made:
Do not use assertions to check the parameters of a public method. An assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled.
It then goes on to say:
You can, however, use an assertion to test a nonpublic method's precondition that you believe will be true no matter what a client does with the class.
Can someone please clarify this issue. I am confused as to how a public method can "guarantee
it will always enforce argument checks". In what way are the arguments checked?
Thanks!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tutorial provides the following example of a public method enforcing the validity of the argument.

If the argument is not valid, then an IllegalArgumentException is thrown. Note that the enforcement of the argument check can not be disabled at run time. In contrast, if the argument were checked with an assertion, then the check could be disabled at run time by not enabling assertions.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the September issue of the JavaRanch newsletter for 2 articles on assertions:
http://www.javaranch.com/newsletter.jsp
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic