From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Create reusable code with methods

Create reusable code with methods - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Create reusable code with methods

- [Instructor] Most programming languages port the ability to create reusable code, code blocks that you can call from multiple places in an application. These are sometimes called functions or subroutines, but in Java, they're called methods and they're always declared as members of a class. The main method is one example. In a command line application, the is the method that's called automatically as you start up a class. But you can create your own custom methods to structure your code however you like. In this beginning project, I have a main method that loops through the list of months and displays one month at a time. And it works fine. When I run the code, the months are displayed. But let's say I wanted to take this bit of code and make it reusable. I'll extract that code to a new method. IntelliJ IDEA has tools that let you do this easily. It's a process called refactor. I'll select the lines of code that I want to extract to a method, then from the menu I'll select refactor,…

Contents