Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was reading tutorial on java which says

1)"Assertions can be used to validate the parameters passed to a private method."
2)"However,assertions should not be used to validate parameters passed to public methods because a public method must check its arguments regardless of whether assertions are enabled or not."

Could any one explain above statements with example code please.........

Thanks in advance,
 
Naveen Kollipara
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reg assertions i think the above two points are about which one is appropriate and which is not.

Have a glance on the following

1)First point is assertions are not going to be there when the application is deployed.

2)Second one is public methods can be called from anywhere and anytime.
If u use assertions the parameters are not going to be validated at
runtime because Refer point one

3)The lastone is private methods r called from with in a class u can assure
abt the values.

hmmm .....

simply private methods are under our control
whereas public ones r not in our hand.

Hope it is cleared and i don't think extra code is required.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Girish, welcome to JavaRanch!

To get better responses in the future, please make your subject lines more descriptive (especially when posting multiple questions on similar topics). Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic