Reputation: 91
I was wondering, how you would call a method with the previous method answers. Let's say: I have multiple classes, the coolNumber() method, and a addNumber method.
A.coolNumber(); //returns 2
addNumber(int i); //add 2
How would you use the answer of 2 as Int for addNumber();? And use it like
A.coolNumber().addNumber();
NOT as: addNumber(coolNumber);
I only simplified the question, in reality I am dealing with a singleton and getters and setters methods.
Upvotes: 1
Views: 253
Reputation: 103
You need to pass in parameters for the method addNumber(). That is how you pass variables to methods. A.coolNumber() can be thought as an int, because it returns the int 2. Therefore you need to do this...
addNumber(A.coolNumber());
//Don't forget the parenthesis after the method call!
Since A.coolNumber returns 2, it will be the same as the following code, given that you don't change the return value in A.coolNumber().
addNumber(2);
Upvotes: 4
Reputation: 11163
You can use the following code snippet -
int someInt = A.coolNumber(); //assign it to someInt, since coolNumber() returns 2.
addNumber(someInt); //use someInt
Or you may use the following code snippet -
addNumber(A.coolNumber());
Upvotes: 1
Reputation: 393801
What you are looking for is:
addNumber(A.coolNumber());
This passes the value returned from A.coolNumber()
as an argument to addNumber
.
P.S, the exact call may be slightly different depending on which classes these two methods belong to, and whether they are static methods or instance methods, which you didn't specify in your question.
Upvotes: 2