Ask coding questions

← Back to all posts
why this code is showing errors and not compiling please help!!!
Abhisheksharm28 (0)

class Main {
public int addThem(int x,int y) {
return x + y;
}
public double addThem(double x,double y){
return x + y;
}
public class NewAdder{
public static void main(String [] args){
Main a=new Main();
int a=23;
int b=34;
int result=a.addThem(a,b);
double doubleresult=a.addThem(22.4,33.5);

    }
}
}
Answered by blonkm (9) [earned 5 cycles]
View Answer
Commentshotnewtop
blonkm (9)

I solved it for you, see https://repl.it/@blonkm/Abhisheksharm28
Your brackets aren't matching up, static wasn't used properly and a was used twice.
Make sure to always call static methods from static methods, and non-static methods from non-static methods. Then you should be ok.

Abhisheksharm28 (0)

@blonkm thankyou so much for the help.. but why did you used static class after the Main class .. please explain

blonkm (9)

@Abhisheksharm28 I did so because Adder was part of Main and Java told me I can't reference Adder from within like that. This is a limitation of Java. So now I have instead moved Adder to its own file, and removed the static keyword. All is still working, proving you don't need static at all (except for the main method, which is called once and thus needs to be defined as static).

katyadee (1134)

Hey there! Make sure add to plenty of context to your posts, so other people can learn from them.

ryanhcode (90)

Could you give us a link to your reply and tell us what it is supposed to do?

Abhisheksharm28 (0)

@ryanhallihan it's a simple program of overloading .. it just need to invoke those methods but when I'm compiling it shows error ..

gammawarrior (0)

Can you send us the link to your repl? @Abhisheksharm28

Abhisheksharm28 (0)

@gammawarrior sorry I will not be able to.. cause I didn't saved it

gammawarrior (0)

We can't really help you if you don't have a repl for us to check out. Can you put it in another repl? @Abhisheksharm28