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.
			
			
									
									
						About Question enthuware.ocajp.i.v8.2.957 :
Moderator: admin
- 
				Javier
 - Posts: 66
 - Joined: Mon Feb 20, 2017 12:31 pm
 - Contact:
 
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v8.2.957 :
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.
			
			
									
									
						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.
- 
				Javier
 - Posts: 66
 - Joined: Mon Feb 20, 2017 12:31 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v8.2.957 :
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!!
			
			
									
									
						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!!
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: About Question enthuware.ocajp.i.v8.2.957 :
That is correct. Since changeIt doesn't return anything, you can't assign its value to int t.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???
Who is online
Users browsing this forum: No registered users and 15 guests