|
hi all. i have an assignment for school that i'm having trouble with. i'm needing to return a value to main from a method. i need that value to equal a value in main. the value in the method is declared as a local variable. how do i call it in main? do i need to declare it again? i'm not sure.
|
|
|
You can save the string in a local variable inside the 'main' method if you need access to it later. Or you can call the method and use the return value without saving it.
class VariableTest
{
public static void main(String[] args)
{
// save return value in a variable
String s = getMessage();
System.out.println("s = " + s);
// use return value without saving it
System.out.println(getMessage());
}
private static String getMessage()
{
return "hello world";
}
}
|
|
|
|
|
|
|
// |