Why the method defined in this is not followed....and only the println variable is getting printed?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why the method defined in this is not followed....and only the println variable is getting printed??

28th Jan 2018, 2:49 PM
Satyam Rai
Satyam Rai - avatar
3 Answers
+ 14
@Satyam 1)see method return type is void ... ie it doesn't return any value 2) & even if it returns then also value returned is not stored in variable x ... ie it is not written x=addOneTo (x);
28th Jan 2018, 3:21 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
where is the code ?
28th Jan 2018, 3:12 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(x); } static void addOneTo(int num) { num = num + 1; } }
28th Jan 2018, 3:13 PM
Satyam Rai
Satyam Rai - avatar