I am learning java. I am getting a lot of errors when I try to keep the methods, which I am trying to invoke, inside the main method.
I am trying to declare a couple of variables x,y. However, I want to invoke the math operations when specific methods are called, such as addMethod, subtractMethod, so on.
When I try to include the methods within the public method, I am getting an error.
package exampleclass;
public class MathLearning {
//declaring variables
static int x = 9;
static int y = 2;
public static void main(String[] args) {
int resu=0;
additMethod(resu);
subtMethod(resu);
multMethod(resu);
divMethod(resu);
private static void divMethod(int resu) {
resu = x+y;
System.out.println(resu);
}
private static void multMethod(int resu) {
resu = x-y;
System.out.println(resu);
}
private static void subtMethod(int resu) {
resu = x*y;
System.out.println(resu);
}
private static void additMethod(int resu) {
resu = x/y;
System.out.println(resu);
}
}
}
When I keep the methods outside the main method, I do not get an error.
package exampleclass;
public class MathLearning {
//declaring variables
static int x = 9;
static int y = 2;
public static void main(String[] args) {
int resu=0;
additMethod(resu);
subtMethod(resu);
multMethod(resu);
divMethod(resu);
}
private static void divMethod(int resu) {
resu = x+y;
System.out.println(resu);
}
private static void multMethod(int resu) {
resu = x-y;
System.out.println(resu);
}
private static void subtMethod(int resu) {
resu = x*y;
System.out.println(resu);
}
private static void additMethod(int resu) {
resu = x/y;
System.out.println(resu);
}
}
why can't I put a method inside another method java. You will save a lot of time in the future if you learn how to ask good questions on google.