File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion usage" Watch "Assertion usage" New topic
Author

Assertion usage

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
As per K&B, we cannot use assertions on public methods & can be used on private methods. What about methods with protected or default access specifiers ?


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You can use assertion any where syntactically. But, assertion is used in testing purposes, so, if you used it in public methods, then it can be used by outsiders.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Sorry Abimaran..Let me modify the question. As per K&B, It is NOT APPROPRIATE to use assertions on public methods &APPROPRIATE to use on private methods. What about methods with protected or default access specifiers ?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:Sorry Abimaran..Let me modify the question. As per K&B, It is NOT APPROPRIATE to use assertions on public methods &APPROPRIATE to use on private methods. What about methods with protected or default access specifiers ?

Do you mean to it appropriately? Or syntactically?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
I am referring "appropriately". syntactically, we can use anywhere(public, private, protected, default).
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

This is from K&B's book,

A public method might be called from code that you don't control (or from code you have never seen). Because public methods are part of your interface to the outside world, you're supposed to guarantee that any constraints on the arguments will be enforced by the method itself. But since assertions aren't guaranteed to actually run (they're typically disabled in a deployed application), the enforcement won't happen if assertions aren't enabled. You don't want publicly accessible code that works only conditionally, depending on whether assertions are enabled. If you need to validate public method arguments, you'll probably use exceptions to throw, say, an IllegalArgumentException if the values passed to the public method are invalid.


So, only within the private method is the appropriate, because You don't want publicly accessible code that works only conditionally, depending on whether assertions are enabled. Others are accessible from outside.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Harikrishna Gorrepati wrote:What about methods with protected or default access specifiers ?


We can use as long as the guidelines/discussions related to Assertions are followed- http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10


Mohamed Sanaulla | My Blog
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

I disagree. You can stick assertions wherever you want.

The point the book makes though, is that you shouldn't rely on them. If you want your code to fail fast and reliably, you need to use exceptions.

Assertions simply make more sense in private methods than in public, default and protected methods, because since you're the only one who has access to the method, you may assume you call the method correctly.

Personally, I rarely find assertions useful. I can imagine they are very helpful in big classes written by multiple programmers, but if I write code by myself and I can take the time to insert the assertions, I can also take the time to make sure my code works correctly.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Stephan van Hulst wrote: I disagree. You can stick assertions wherever you want.

I agree that you can use assertions wherever possible. I thought of letting the OP know why/why not to use assert.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
It's important to remember the context of this thread...

For the sake of the exam (this is the SCJP forum after all), this discussion should revolve around what Sun / Oracle says about the *appropriate* use of assertions.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion usage
 
Similar Threads
servlet security
Lucene - get TermVector positions
How do OBJECTS pass messages in Java?
Inheritance of all members
What is the logic behind calls like JOptionPane.method()