File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assertion 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 "assertion" Watch "assertion" New topic


Maciek Zywno

Joined: Jul 29, 2003
Posts: 2
I tried the following piece of code :
public class AssertTest {

public void methodA( int i ){
assert i >= 0 : methodB();
System.out.println( i );

public int methodB(){
System.out.println( "The value must be nonnegative" );
return 1;

public static void main(String[] args) {
AssertTest test = new AssertTest();
test.methodA( -10 );
As I execute it only "-10" is printed and no print from methodB().
I wonder why methodB() is not invoked. Sun's docs say that in
assert Expression1 : Expression2 ;
expression2 cannot be the invocation of a method that is declared void.
In "my" case methodB() is not of a void type, so why it is not executed?
If it is not executed how the expression2 is evaluated?
what am I missing?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
You must run it with assertion flag on:
java -ea AssertTest
I agree. Here's the link:
subject: assertion
It's not a secret anymore!