Creating Methods in Java
A Simple Thought
Imagine that every time you needed to tie your shoes, you had to learn the steps from scratch. That would take forever.
Instead, you learned the process once, and now you can repeat it whenever you want.
Programs work the same way. Instead of writing the same code again and again, you can put the steps inside something called a method and use it whenever needed.
In this lesson, you will learn how to create methods in Java and call them to make your programs organized and reusable.
The Project: Greeting Program
You will create a simple program that prints a greeting message to the screen.
The greeting will be printed using a custom method that you create.
Your output should look like this:
1 2 3 4======================== GREETING PROGRAM Welcome to Java Programming! ========================
The greeting will come from a separate method named printGreeting.
The Concept: Methods
A method is a block of code that performs a specific task.
You can think of it as a small function inside your program that you can call anytime you need it.
Here is the basic structure of a method in Java:
1 2 3public static void methodName() { // code to run }
- public means it can be used anywhere in the program.
- static means it belongs to the class and can be called directly.
- void means the method does not return any value.
- methodName is the name you give to the method.
You can call a method by writing its name followed by parentheses, like this:
methodName();
Why This Matters
Methods help you avoid repeating code and make programs easier to manage.
They are used everywhere:
- A calculator uses methods to add, subtract, multiply, and divide.
- A website might have one method for showing messages and another for saving data.
- A game might have separate methods for starting, scoring, and ending.
Using methods keeps your code clean, readable, and flexible.
Let’s Build the Greeting Program
Here’s the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14public class GreetingProgram { public static void main(String[] args) { System.out.println("========================"); System.out.println(" GREETING PROGRAM"); printGreeting(); System.out.println("========================"); } public static void printGreeting() { System.out.println(" Welcome to Java Programming!"); } }
When you run this program, it will print the greeting from the printGreeting method.
The main method calls it once, but you could call it again anywhere in the program.
How It Works
- The program starts in the main method.
- It prints the header, then calls the printGreeting() method.
- The program moves to the printGreeting method and executes its code.
- After finishing the method, the program returns to main and continues running.
This process of calling and returning makes your program flow smoothly between different blocks of code.
Learn Together with AI
If you are using an AI Copilot, you can ask it to write and modify methods for you.
Try this prompt:
“Write a Java program that defines a method called printGreeting which displays a welcome message.”
After running it, you can ask for changes such as:
- “Add another method that prints a goodbye message.”
- “Make the greeting method print the message five times using a loop.”
- “Add a method that takes a name and prints a personalized message.”
Each variation helps you see how methods can work with each other to make your program more powerful.
Practice Challenge
Create a program that has two methods, one to print “Good Morning!” and another to print “Good Night!”.
Call both methods from the main method and make sure your output looks like this:
1 2 3 4 5======================== DAILY GREETINGS Good Morning! Good Night! ========================
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19public class DailyGreetings { public static void main(String[] args) { System.out.println("========================"); System.out.println(" DAILY GREETINGS"); goodMorning(); goodNight(); System.out.println("========================"); } public static void goodMorning() { System.out.println(" Good Morning!"); } public static void goodNight() { System.out.println(" Good Night!"); } }
When you run this program, both greetings will appear in the correct order.
You have now created your own reusable pieces of code that can be called anytime.
Coming Up Next
You have learned how to create and call methods that perform specific tasks.
Next, you will learn how to pass information to methods using parameters, and how methods can send results back using return values.