CodeGym /Courses /JAVA 25 SELF /Declaring and calling methods, parameters

Declaring and calling methods, parameters

JAVA 25 SELF
Level 8 , Lesson 2
Available

1. Method declaration syntax

Declaring a method in Java is like a recipe in a cookbook: you specify what the method returns (type), what it’s called, what ingredients (parameters) it needs, and write the actual recipe (the method body).

General form:


return_type methodName(parameters)
{
    // write commands here
}
General method declaration form
  • return type — what the method returns (for example, int, void — if it returns nothing, String, etc.).
  • method name — the name you come up with (following Java rules).
  • parameters — the list of variables the method will use (may be empty).
  • method body — your statements inside curly braces { }.

Examples of methods without parameters

void printHello() 
{
    System.out.println("Hello, world!");
}

Explanation: void — the method returns nothing; printHello — the name; () — no parameters.

Example of a method with parameters

void printName(String name) 
{
    System.out.println("Hello, " + name + "!");
}

Explanation: void — returns no value; printName — the method name; String name — a single parameter of type String.

Example of a method with multiple parameters

void printSum(int a, int b) 
{
    System.out.println("Sum: " + (a + b));
}

Explanation: parameters are listed separated by commas. Inside the method we use them like regular variables; the + operator performs addition.

Important!

  • Methods are declared inside a class, but NOT inside other methods.
  • You can’t declare methods inside other methods in Java — no “matryoshkas”.

2. Calling a method

Declaring a method is like hanging a sign that says “I’ll make coffee.” But for the coffee to actually appear, you need to call the method — write its name and pass parameter values.

How to call a method

If the method is in the same class and declared static (like main), you can call it by name:

printHello();
printName("Alice");
printSum(3, 5);

If a method is not static, it must be called through an object (more on this later when we study classes and objects).

Example: calling a method from main


public class MethodsDemo
{
    public static void main(String[] args) 
    {
        printHello();
        printName("Bob");
        printSum(10, 20);
    }

    static void printHello() 
    {
        System.out.println("Hello, world!");
    }

    static void printName(String name) 
    {
        System.out.println("Hello, " + name + "!");
    }

    static void printSum(int a, int b) 
    {
        System.out.println("Sum: " + (a + b));
    }
}

Note: the methods are declared with the static modifier so they can be called from main (which is also static).

3. Parameters vs. arguments: what are they?

Parameters are variables declared in parentheses when declaring a method.
Arguments are the actual values you pass when calling the method.

Analogy: Parameters are like a shopping list; Arguments are what you actually put in the cart.

Example


      static void greet(String name)  // name is a parameter
{
    System.out.println("Hello, " + name + "!");
}

public static void main(String[] args) 
{
    greet("Katya"); // "Katya" is an argument
    greet("Vasya"); // "Vasya" is an argument
}

When you call greet("Katya"), inside the method the variable name will be equal to "Katya".

Multiple parameters


      static void printInfo(String name, int age)
{
    System.out.println("Name: " + name + ", age: " + age);
}

public static void main(String[] args) 
{
    printInfo("Masha", 25);
    printInfo("Petya", 31);
}

Attention! The types and order of parameters must match how you declared the method.

4. Practice: writing your own methods

A method that prints a greeting with the user’s name


static void sayHelloTo(String name)
{
    System.out.println("Hello, " + name + "!");
}

public static void main(String[] args)
{
    sayHelloTo("Andrey");
    sayHelloTo("Elvira");
}

A method that calculates the sum of two numbers


static void printSum(int a, int b)
{
    int sum = a + b;
    System.out.println("Sum: " + sum);
}

public static void main(String[] args) 
{
    printSum(7, 8);
    printSum(15, -5);
}

A method that prints array elements


      static void printArray(int[] array)
{
    System.out.print("Array: ");
    for (int i = 0; i < array.length; i++) 
    {
        System.out.print(array[i] + " ");
    }
    System.out.println();
}

public static void main(String[] args) 
{
    int[] nums = {1, 2, 3, 4, 5};
    printArray(nums);
}

A method that prints the same string n times


      static void repeatPrint(String text, int n)
{
    for (int i = 0; i < n; i++) 
    {
        System.out.println(text);
    }
}

public static void main(String[] args) 
{
    repeatPrint("Java is awesome!", 3);
}

5. How methods help structure an application

If you decide to write a simple console game or calculator, the code will be clearer if each task is encapsulated as a method.

Example: mini-calculator


public class Calculator
{
    public static void main(String[] args) 
    {
        printGreeting();
        printSum(3, 7);
        printDifference(10, 4);
    }

    static void printGreeting() 
    {
        System.out.println("Welcome to the calculator!");
    }

    static void printSum(int a, int b) 
    {
        System.out.println("Sum: " + (a + b));
    }

    static void printDifference(int a, int b) 
    {
        System.out.println("Difference: " + (a - b));
    }
}

In the future you can add new methods (multiplication, division, working with arrays of numbers, etc.), and the application will grow without turning into a long wall of code.

6. Useful nuances

Method names

  • A method name usually starts with a lowercase letter and uses camelCase: printSum, sayHelloTo, calculateArea.
  • The name should reflect the action. Avoid doStuff and method1.

Parameter order

  • Keep the order and types of parameters when calling.
  • If a method takes printInfo(String name, int age), you cannot call printInfo(25, "Masha").

Can you declare two methods with the same name?

Yes, if they have different parameter lists (different types or counts). This is method overloading:

static void printSum(int a, int b) { /* ... */ }
static void printSum(int a, int b, int c) { /* ... */ }

We’ll cover this in a separate lecture — for now it’s enough to know that Java doesn’t limit you to a single method per name.

7. Typical mistakes when working with methods and parameters

Error #1: Mismatch in the number or types of parameters when calling.
If the method is declared as printSum(int a, int b), and you call it as printSum(5), the compiler will say that no such method exists. Similarly, printSum("five", 7) — type error.

Error #2: Confusing parameters with arguments.
In a method declaration you write parameters (e.g., int a, int b), and when calling — arguments (printSum(5, 7)). You don’t need to create variables with the same names in main — what matters is matching types and order.

Error #3: Trying to declare a method inside a method.
In Java, methods are declared only at the class level. You cannot declare one inside another method.

Error #4: Not using static when calling from main.
If you call a method from main, add static to its declaration or create an object and call the method on it. Details — later, when studying OOP.

1
Task
JAVA 25 SELF, level 8, lesson 2
Locked
Personal greeting for a new user 👋
Personal greeting for a new user 👋
1
Task
JAVA 25 SELF, level 8, lesson 2
Locked
Quick calculation of the purchase cost 💰
Quick calculation of the purchase cost 💰
1
Task
JAVA 25 SELF, level 8, lesson 2
Locked
Creating an employee card for HR 👩‍💻
Creating an employee card for HR 👩‍💻
1
Task
JAVA 25 SELF, level 8, lesson 2
Locked
Designing Various Shapes and Sizes of Objects 📐
Designing Various Shapes and Sizes of Objects 📐
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION