JavaRush /Java Blog /Random EN /Methods [chapter 1]

Methods [chapter 1]

Published in the Random EN group
Programmers once came up with the concept of object-oriented programming, which allows us to reflect the objects of the domain with which we work in program code. Together we will begin to get acquainted with the basics of this concept, learn what objects, classes and methods are in the Java language and see with practical examples how to create them, how they work and how they can be used. Methods In this short article we will talk about what a method is, what methods there are and what they are used for. A method is essentially a piece of code that performs some action, in other words, a method is a block of code that does some work and is called something. The executable code in the method is in curly braces, and at the beginning there is a certain line written, consisting of some words and parentheses. Let's take a closer look at what parts this line consists of.
public static void main(String[] args)
private static boolean checkRange(int income)
private static int calculateCharges()
At the beginning there is either the word private or the word public - this is the so-called access modifier , which establishes where this method can be called from. There are 4 types of access modifiers : 1) public 2) private 3) protected 4) – The fourth type, in which there is no access modifier. Later we’ll look at how access modifiers affect the availability of methods in the code, but for now let’s just remember what this first word could be. The second word in all our methods is the word static . The methods may or may not have it. We’ll also talk about it a little later, but for now let’s just remember that it can occur. The third word is either some data type ( int or boolean as in the example above) or the keyword void . Methods always have this third word, we will also talk about it a little later. What we'll talk about now is the method name , in this case the fourth word. But you understand that if there is no word static (which may not exist), then the method name will be the third word, there may not yet be an access modifier, then the method name will be word number two. When you write your code, you can give any method names , as long as they reflect the essence of what the method does. Since the method performs some action, it is desirable that this action be present in its name. Methods in Java are usually named with a small letter, and if the name of a method consists of several words, then write all the words together, the second and each subsequent word starting with a capital letter, and the program must have at least one method named main, since it is program execution begins with this method. Another important component of the method is parentheses . They may contain so-called parameters . These are the types and names of variables that can be passed to the method and which will then be used in it. There may be no parameters and then the brackets are written empty. There may be one parameter, as in our example (variable type and its name), or there may be several parameters (separated by commas). We’ll talk about the parameters in detail and separately. For now, you just need to know that methods can have them. The name of a method and the set of parameters that are passed to that method in a specific order is called the method signature. It should be noted that the order of the parameters also matters; if the parameters are passed in a different order, then it will be a different method. So, we started getting acquainted with the methods and realized that the method- this is a certain fragment of program code that has a number of properties and a name by which this method can be called from other code. In the next article we will talk about method parameters.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION