Nested calls return in reverse order
Each call pauses its caller. The last method entered is the first to finish.
A method is a named block of code you can run again and again by calling its name. Methods let you write a piece of work once, give it a clear name, and reuse it, which keeps programs short, readable and easy to fix.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a program with a method printLine() that prints 20 dashes, and use it to print a framed message: a line, Welcome to Java, a line.
Write printSquare() that prints a 3x3 block of # using two nested loops, and call it twice with a blank line in between.
Write three methods morning(), afternoon() and evening() that each print a greeting, and a method fullDay() that calls all three in order. Call fullDay() from main.
Explain it without notes
What happens, step by step, when main calls a method and that method finishes?
Why does duplicating code instead of writing a method cause bugs over time?
Why can't main call a method that isn't marked static?
What makes a good method name? Give two good and two bad examples.
Expected output
enter main
enter a
enter b
in c (deepest)
back in b
back in a
back in main