Here's an overview of the classes that Java cannot avoid. You can read it if you know variables and simple calculations. On the contrary, deep things are not introduced in this article. Maybe it's rare to write in this way?
Classes make Java, leaving the exact definition of classes to other articles and books. In fact, various Java features are defined in classes. And by creating your own class, you can create the functions you want.
Now let's actually configure the class. Here, let's create a Test class. Classes have processes called variables and methods. Let's make a Test class in Japanese.
public class Test{
/*
*Declare class variables here and there
*/
/*
*Here, the processing that you want to realize in the class'Method'Define
*/
}
The variables of the class are written as follows.
private int a = 4;
Here, 4 is put in an integer type variable called a. Variables often use the following two naming rules:
Write the name of the variable. Although it is not a Java rule, the variable name must be something that represents the content of the process or operation. Give it a name that others can easily see. LowerCamel Also, in general, it tends to be attached by connecting English words, but uppercase and lowercase letters are generally fixed. Generally, the first English word is written in lowercase, and the second and subsequent letters are written in uppercase.
Correct example javaQiita, qiitaJava
bad example javaqiita, JavaQiita
By the way, private is explained together in the method description.
Next is the method. The method describes such processing and the operation of the variable you want to perform. And finally, it returns the result of processing and operation. The method has the following structure.
public Return type Method name(Argument type Argument){
/*
*The contents of the method
*Write the process or operation you want to achieve with the method.
*/
return Return value;
}
Let's explain one by one
The result returned by the method is called the return value. And the method first writes what type of value the return value is. Value at the end of processing
return Return value;
Write. By the way, if the method executes this return, it will end there.
Method names have the same naming rules as variables.
The method may also contain some value. For example, the process of multiplying a certain number by 3 or the process of adding 4 to a certain number. This certain number is entered as an argument. Arguments use the same naming convention as variables.
Now let's touch on the access specifier. The access specifier is private or public of the method or class that appears when the variable is used. This refers to the range that can be used for that variable or method. public You can use this method or variable from other classes. private You can use methods and variables only from within your class. For beginners, it's safe to remember that classes and methods are public and variables are private.
For example, create a method that triples the argument and returns it.
public int multipleIntegerNumber(int num){
num = 3*num;
return num;
}
Here, I introduced how to write the contents of the class. I will explain how to actually use the class in the next article. Classes and instantiations (please wait a moment) Also related to access specifiers Encapsulation and setter, getter (please wait a moment)
Recommended Posts