This is my code:
public void viewFlight() {
int select;
String option;
String newOrigin = null;
viewFlightOrigin();
}
public void viewFlightOrigin() {
option = console.nextLine();
switch (select) {
case 1:
System.out.println("=======================================");
System.out.println("city of origin: Melbourne");
System.out.println("=======================================");
newOrigin = "Melbourne";
break;
// ...
}
}
How to use local variables in the viewFlight() to be used again in the viewFlightOrigin() without declaring the variables in the field or re-declaring again in the viewFlightOrigin()?
The easiest thing to do if you do not need to modify the variable would be to pass it down as a parameter to the function:
If you need to modify the variable, you can return the new value from the method: