aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why ?A  assertion question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why ?A  assertion question." Watch "why ?A  assertion question." New topic
Author

why ?A assertion question.

Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
Assertions should not be used to validate arguments passed to public methods.
but,
Assertions can be used to validate arguments passed to nonpublic methods.


SCJP1.4 <br />Best wishes!<br />中国人!
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- great question!
The idea that it is "not appropriate" to validate arguments to a public method (as opposed to it being "appropriate" to validate args to a private method) is because with a *public* method, you're exposing that method to the outside world, so you had better be taking care of the validation in your code (including use of exceptions if necessary).
In other words, if the method is public, then code *you* did not write and cannot control can invoke that method, so you must protect it with programmatic validation. But... if it is a *private* method, then *you* are the only one who is writing the code that invokes that private method, so it's OK to use assertions to test what you are assuming that *you* have sent in.
So that's the main difference. Remember that both are legal (can compile and run), but the exam expects you to know what is and is not an "appropriate" use of assertions, and this is one example (public vs. private method arg validation).
Cheers,
Kathy
(working on becoming more assertive )


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
thanks! :-)
 
 
subject: why ?A assertion question.