Lesson 18 of 34

Methods: Reusable Code

9 min 25 XP

A method is a named block of code: write it once, call it many times. For now we'll write static methods next to main(). Parameters pass values in.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

void means the method doesn't give back a value. Next lesson: methods that return values.

Challenge

+25 XP

Write a method printBox(String text) that prints: [ text ] In main(), read a line of input and call printBox with it. Input: Hello Output: [ Hello ]

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.