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 - Java Tutorial
From the course: Java 8+ Essential Training: Syntax and Structure
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,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Evaluate conditions with if-else4m 17s
-
Evaluate conditions with switch-case4m 32s
-
(Locked)
Create looping code blocks7m 13s
-
(Locked)
Create reusable code with methods8m 17s
-
(Locked)
Create overloaded methods3m 27s
-
(Locked)
Pass arguments by reference vs. value4m
-
(Locked)
Challenge: A more complex calculator1m 18s
-
(Locked)
Solution: A more complex calculator2m 20s
-
(Locked)
-