Basically, I am trying to make methods to return a value when they are called from another method or class. I was wondering what conditions and rules I have to follow to make them work. If you want an example of what I meant, just ask me and I will update the post.
Here is an example of what I have done. I have created two classes. In the first class, I have tried to make a method in another class to return a value with a command to output value on the console. But I get an error that says there are incompatible types. Here are two classes that I have created and I wanted to make a calculator out of that: The first class:
Code:
class calc1
{ public static int num1; //first number variable
public static int num2; //Second number variable
public static String op; //Operatior variable
public static void main(String[] args) //
{ num1 = Integer.parseInt(args[0]);
num2 = Integer.parseInt(args[2]);
op = args[1];
calc3.calculate(op); //Calling method from the second class with an arugement.
}
}
Here is the second:
Code:
class calc3
{ public static int calculate(String ops)
{
switch(ops) //I believe that ops stores value from op variable in the first class.
{ case "+": { int num = calc1.num1 + calc1.num2;
return (System.out.println(num));
}
}
}
}
Here is an error message I get after I try to compile it:
Code:
Desktop$ javac calc1.java
./calc3.java:10: error: incompatible types return (System.out.println(num));^
required: int
found: void
1 error
PS. I believe it is not overriding process, but still I would like to know about calling methods and returning value when they are called.
Bookmarks