JavaRush /Java Blog /Random EN /What are methods in Java?
Мариам
Level 10

What are methods in Java?

Published in the Random EN group
What are methods in Java?  - 1Here's what an example of a simple method looks like:
public static void summa (int x, int y){
       int z = x + y;
       System.out.println("Summa chisla " + x + " i " + y + " = " + z );
   }
In fact, a METHOD is a piece of code that can be called, and it will do what is written inside it (calculate, print, etc.). The NAME of my method is summa. TASK - add two numbers and display text about it. When you need to add two numbers, just write summa(7, 3) or summa(54, 352). The whole code looks like this:
//Класс "Математика"
public class Matematika{
    public static void main(String[] args) {
        summa(5, 10); // Вызывается метод для чисел 5 и 10
        summa(222, 111); // Вызывается метод для чисел 222 и 111
    }
    //ниже уже сам метод summa
    public static void summa (int x, int y){
       //метод складывает полученные цифры....
        int z = x + y;
        //... и распечатывает
        System.out.println("Summa chisla " + x + " i " + y + " = " + z );
    }
}
You might think, WHY do you need a method when you can just add numbers in code every time you need to add? The fact is that a simple short example is given here, therefore the method is short. In real life, the method can be large, and it's not comme il faut to repeat it in the code over and over again, because THIS WILL MAKE your code unwieldy, slow, and most likely you will make a typo somewhere. Therefore, we came to the conclusion that for repetitive things it is better to use methods. WHEN WRITING THE METHOD: (Advice for beginnersprogrammers) 1) Pay attention to the PLACE in the code where you insert your method. There is no where to enter it. You can enter the method UNDER the class name, but BEFORE "public static void main(String[] args)", well, or like mine. 2) before the name (summa) of my method it is written public static void. In your first programs, it is better to write this way. And you will become more experienced, and there you will find out what other words you can write before the name of the method. If you really want to know what these words mean, then here: - PUBLIC means that all other classes can call your method. - STATIC allows a method to be called from "public STATIC void main(String[] args)". Here are such whimsical static methods - they can only communicate with their own kind. And as soon as the method is NOT static, they turn their noses up and refuse to be called. - VOID means that the method will not return anything. It's just that methods can not only accept numbers and do something with them, but they can also return a number. Then instead of void it is necessary to write the type of the returned variable. A method can return anything. Learn ALL about methods from HerbertSchildt in Chapter 6. About methods in English, see the video
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION