Debug the error and please explain !! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Debug the error and please explain !!

public class MyClass { public static void main(String[ ] args) { int x = 5; int y = addOneTo(x); System.out.println(y); } static void addOneTo(int x) { x = x + 1; } }

11th Sep 2016, 6:26 PM
Sourabh jain
Sourabh jain - avatar
4 Answers
+ 2
your return type for addOneTo method is void but in int y=addOneTo(x); statement you are trying to assign your void type to int type. so error occurs.. To fix it declare addOneTo method as int type
13th Sep 2016, 5:10 PM
Teja Naraharisetti
Teja Naraharisetti - avatar
0
How
11th Sep 2016, 6:30 PM
Dinesh
0
static int addOneTo(int x) { return x + 1; }
11th Sep 2016, 6:54 PM
Zen
Zen - avatar
0
i know the answer but why? why can't we use void type? methods can be of any type..
12th Sep 2016, 12:25 PM
Sourabh jain
Sourabh jain - avatar