t.amethod() is an expression statement and the value is discarded. Quoting from JLS
14.8 Expression Statements
Certain kinds of expressions may be used as statements by following them with semicolons:
ExpressionStatement:
StatementExpression ;
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
An expression statement is executed by evaluating the expression; if the expression has a value, the value is discarded. Execution of the expression statement completes normally if and only if evaluation of the expression completes normally.
here t.amethod() is MethodInvocation and the value is discarded.
Originally posted by Bin Wang:
public class Test {
public static void main(String[] args) {
Test t=new Test();
t.amethod();
}
int amethod () {
return -1;
}
}
The program compile and run (putput nothing). So it's not necessary to assign t.amethod to an int variable?
can somebody explain this? Thanks.
[This message has been edited by Anshul Manisha (edited June 26, 2001).]