0
0
Javaprogramming~15 mins

Method declaration in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeMethod declaration
📖 Scenario: You are creating a simple Java program to greet users by name. This program will help you understand how to declare and use methods in Java.
🎯 Goal: Build a Java program that declares a method called greetUser which takes a String parameter called name and prints a greeting message including that name.
📋 What You'll Learn
Declare a method named greetUser that takes one String parameter called name
The method should print the message: Hello, <name>! where <name> is the parameter value
Call the greetUser method from the main method with the argument "Alice"
Print the greeting message to the console
💡 Why This Matters
🌍 Real World
Methods are used in all Java programs to organize code into small, reusable pieces that perform specific tasks.
💼 Career
Understanding method declaration is essential for any Java developer, as it is the foundation for writing clean, maintainable, and reusable code.
Progress0 / 4 steps
1
Create the main class and main method
Create a public class named Greeting and inside it, write the main method with the exact signature public static void main(String[] args).
Java
💡 Need a hint?

The main method is the entry point of a Java program. Use the exact signature public static void main(String[] args).

2
Declare the greetUser method
Inside the Greeting class but outside the main method, declare a public static method named greetUser that takes a String parameter called name and returns nothing (void).
Java
💡 Need a hint?

Method declaration syntax: public static void greetUser(String name) means the method is public, static, returns nothing, and takes one String parameter.

3
Add print statement inside greetUser method
Inside the greetUser method, write a System.out.println statement that prints the message Hello, <name>! using the parameter name with string concatenation.
Java
💡 Need a hint?

Use System.out.println("Hello, " + name + "!") to print the greeting message.

4
Call greetUser method from main
Inside the main method, call the greetUser method with the argument "Alice" to display the greeting message.
Java
💡 Need a hint?

Call the method by writing greetUser("Alice"); inside the main method.