How to Call a Method That Returns a String in Java
Java methods can return data values and variables of different types, including text strings. If you need to call a method returning a string, the end result will be a string value your program can use later. How you call a method depends on its outline. You need to use the method name and provide any parameters it requires. Your method calls must specify the correct number and type of parameters. Once the method executes, you can store the resulting string value in a variable.
Instructions
-
-
1
Look at the signature of the method you wish to call. The method signature includes its name, parameters, return type and visibility. Your programs can only call methods in other classes if they have public visibility. If you are calling a method from a class, you need to have an instance of that class. The following example code demonstrates declaring and instantiating an object instance of a class:
UsefulHelper help = new UsefulHelper();This causes the constructor method of the class to execute, returning an object instance.
-
2
Use the method name to call it. Start with the name of your object instance, then follow it with the method name as in the following example code:
help.getTextThis would apply to a method with the name "getText," which is implemented inside the "UsefulHelper" class declaration. If the method has no parameters, you can call it as follows:
help.getText();If the method requires parameters, you need to include these.
-
-
3
Check what parameters your method call needs to supply. The following sample method outline indicates two parameters, an integer and a string:
public String getText(int num, String word)To call this method, you need to include these parameters, as follows:
help.getText(3, "apple");The following code has the same effect, but uses variables instead of values:
int number = 3;
String wordText = "apple";
help.getText(number, wordText); -
4
Store the return value of your method call in a variable. Alter your method call as follows:
String helpText = help.getText(number, wordText);When the method executes, it will return the string value to the place it was called from, then Java will store the value at the specified variable reference. Your code can refer to the string variable in subsequent processing.
-
5
Test your method call. Add the following line to test the functionality of your method calling code:
System.out.println("Help Text: " + helpText);You will be able to see at a glance whether your code has worked. Save and compile your file, and then run a test. Experiment with the code by altering the parameter values to see what effect they have on the resulting string.
-
1
Tips & Warnings
If you program in an Integrated Development Environment (IDE), it may prompt you with information about method parameters.
Make sure you understand the purpose of a method before calling it in case it has unintended consequences.
References
Resources
- Photo Credit Thinkstock Images/Comstock/Getty Images