Page 1 of 1
About Question enthuware.ocajp.i.v8.2.957 :
Posted: Mon Apr 17, 2017 8:19 am
by Javier
Hi Paul!
Why is not compiling this method?
public static void changeIt(int a){
a+2;
}
and this neither?
public static void changeIt(int a){
int f=4;
a+f;
}
I think that something very basic I am missing but I don´t know exactly what...
Thank you very much Paul.
Re: About Question enthuware.ocajp.i.v8.2.957 :
Posted: Mon Apr 17, 2017 9:13 am
by admin
As per section 14.8 of JLS the only expression statements that are valid in Java language are:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
a+2; or a+f; are, therefore, not valid although a = a+2; or a = a+f; are valid because they are valid assignments.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.957 :
Posted: Mon Apr 17, 2017 9:16 am
by Javier
and why is not possible to store the value of the method:
changeIt(someInt);// like int t= changeIt(someInt);
is it not possible because changeIt is void method???
Thank you so much Paul for the quick answer to my last question!!
Re: About Question enthuware.ocajp.i.v8.2.957 :
Posted: Mon Apr 17, 2017 9:35 am
by admin
Javier wrote:and why is not possible to store the value of the method:
changeIt(someInt);// like int t= changeIt(someInt);
is it not possible because changeIt is void method???
That is correct. Since changeIt doesn't return anything, you can't assign its value to int t.