Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Method Declaration and Invocation | Introduction to Methods
Mastering Methods in Java

bookMethod Declaration and Invocation

Declaring Your Own Methods in Java

To create your own method in Java, you need to follow a specific syntax and set of rules. A method is a block of code that performs a specific task and can be called from other parts of your program. Every method declaration includes several key components:

  • Access modifier: defines the visibility of the method, such as public or private;
  • Return type: specifies the type of value the method will return (use void if nothing is returned);
  • Method name: must follow Java naming conventions—start with a lowercase letter and use camelCase;
  • Parameter list: enclosed in parentheses; can be empty or contain one or more parameters separated by commas;
  • Method body: the block of code inside curly braces {} that defines what the method does.

General syntax:

accessModifier returnType methodName(parameterList) {
    // method body
}

Example:

public void greetUser() {
    System.out.println("Hello, user!");
}

This method is named greetUser, is declared as public, has no return type (uses void), and doesn't take any parameters. When you call this method, it simply prints a greeting message to the console.

Note
Note

You'll learn about method parameters a little later.

Rules for method declaration:

  • Method names must be unique within the same class (except for overloading);
  • Parameter types and order must be clearly defined;
  • The return type must match the value returned in the method body;
  • If the method does not return a value, use void as the return type.

Declaring methods helps you organize your code, avoid repetition, and make your programs easier to read and maintain.

Invoking a Method in Java

To use a method in Java, you must invoke (call) it from another method. Most commonly, you will call methods from the main method or from other methods within your class.

Syntax for Method Invocation

To invoke a method, use the following syntax:

  • Type the method name;
  • Add parentheses () after the name;
  • End the statement with a semicolon.

If the method returns a value, you can use it directly or assign it to a variable.

Main.java

Main.java

copy
1234567891011121314
package com.example; public class Main { // Step 1: Declare a method named greet public static void greet() { // Step 2: Print a greeting message System.out.println("Hello from the greet method!"); } public static void main(String[] args) { // Step 3: Invoke the greet method greet(); } }
  • Methods are invoked using their name followed by parentheses;
  • You can call a method from main or from any other method in the same class;
  • If the method returns a value, you can assign it to a variable or use it in an expression.
question mark

Which of the following is a valid method declaration in Java

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me an example of invoking a method with parameters?

What happens if I try to return a value from a void method?

Can you explain method overloading in Java?

Awesome!

Completion rate improved to 8.33

bookMethod Declaration and Invocation

Свайпніть щоб показати меню

Declaring Your Own Methods in Java

To create your own method in Java, you need to follow a specific syntax and set of rules. A method is a block of code that performs a specific task and can be called from other parts of your program. Every method declaration includes several key components:

  • Access modifier: defines the visibility of the method, such as public or private;
  • Return type: specifies the type of value the method will return (use void if nothing is returned);
  • Method name: must follow Java naming conventions—start with a lowercase letter and use camelCase;
  • Parameter list: enclosed in parentheses; can be empty or contain one or more parameters separated by commas;
  • Method body: the block of code inside curly braces {} that defines what the method does.

General syntax:

accessModifier returnType methodName(parameterList) {
    // method body
}

Example:

public void greetUser() {
    System.out.println("Hello, user!");
}

This method is named greetUser, is declared as public, has no return type (uses void), and doesn't take any parameters. When you call this method, it simply prints a greeting message to the console.

Note
Note

You'll learn about method parameters a little later.

Rules for method declaration:

  • Method names must be unique within the same class (except for overloading);
  • Parameter types and order must be clearly defined;
  • The return type must match the value returned in the method body;
  • If the method does not return a value, use void as the return type.

Declaring methods helps you organize your code, avoid repetition, and make your programs easier to read and maintain.

Invoking a Method in Java

To use a method in Java, you must invoke (call) it from another method. Most commonly, you will call methods from the main method or from other methods within your class.

Syntax for Method Invocation

To invoke a method, use the following syntax:

  • Type the method name;
  • Add parentheses () after the name;
  • End the statement with a semicolon.

If the method returns a value, you can use it directly or assign it to a variable.

Main.java

Main.java

copy
1234567891011121314
package com.example; public class Main { // Step 1: Declare a method named greet public static void greet() { // Step 2: Print a greeting message System.out.println("Hello from the greet method!"); } public static void main(String[] args) { // Step 3: Invoke the greet method greet(); } }
  • Methods are invoked using their name followed by parentheses;
  • You can call a method from main or from any other method in the same class;
  • If the method returns a value, you can assign it to a variable or use it in an expression.
question mark

Which of the following is a valid method declaration in Java

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
some-alt