Jump to content

something confusing about assertion in Java...

Featured Replies

Just a simple question about assertion...

 

I heard a statement that assertion is most appropriately used to precondition that checks for a non-public method... such as

 

[b]protected/private/[i]default[/i][/b] void addDevil(String devilName)
{assert isValidDevilName(devilName);}

 

but isn't appropriate for public method.

 

 

Why?????????

 

please help

 

thanks

http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html#usage-conditions

 

It explains here why. It sounds like it's because with a public method, it will go on to be used by another class or even another coder and will be used alot more outside of the general scope of this class. Asserts are generally not run at runtime (afaik) and you have to specify during runtime that you want them enabled. This is great for debugging but when you have finished, you will still need your public methods to check the validity of parameters passed in, even outside of debugging, because another class or even another coder might pass in a value that is unacceptable, whereas the private and protected methods are only being passed parameters from within the same class or at least a very limited scope, and so you can control the parameters that they are passed and the assertions that their arguments are valid should hold true, as long as you check to make sure that anything that might change this from outside (ie when arguments/parameters are passed to a public method) are valid.

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.