Good evening. Again. It's been 3 days since I started writing the article. I'll do my best today too.
Today I will write about encapsulation. Encapsulation is to clarify the interface to access the created class. The scope of encapsulation and the specification method are shown below. We will use a very easy-to-understand table in the references.
name | How to specify in the program | Scope of permission for access |
---|---|---|
private | private | Only my own class |
package private | Write nothing | Classes that belong to the same package as you |
protected | protected | Child classes that belong to the same package as you or inherit from you |
public | public | All classes |
I think I used to say public when writing the main method, but this uses encapsulation. The main method is a method that is called when the program is executed, so if you make this part private, an error will occur and it will not be executed.
Now, let's explain static using an example. I will not touch on protected this time because I will write it when explaining inheritance.
Num.java
public class Num{
static int a = 10;
}
At this time, if you try to call the field defined in private with the main method of another class, an error will occur and it will not be executed. To do this, we will create methods called getters and setters.
Num.java
public class Calc{
static int a = 10;
public int getA(){
return this.a;
}
public void setA(int a){
this.a = a;
}
}
By using this getter and setter method, you can call and write the values of fields a and b of class calc.
If you get tired of making everything public, the value of the field will be rewritten in an unexpected place and no error will occur, so I think it is better to use private for the field if possible. At the same time, in order to distinguish between the method argument and the variable defined in the class, I think it is better to add this to the field defined in the class.
I had a lethargy syndrome today and wasn't doing anything. To be honest, I was worried about writing an article. .. .. I am sorry that the amount is small because I can not output because there is no input. .. .. However, I was able to write it when I was enthusiastic, so I will continue to do my best to write in this condition. Next time, my juniors will ask me about my progress, so I will write about Vue.js, which I recently started studying. I will do my best to write it so that even those who do not know Vue.js can understand it. I would appreciate it if you could point out any mistakes or differences in expressions in this article.
[Introduction to Java 2nd Edition (Refreshing Series)](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA % E3% 82% 8F% E3% 81% 8B% E3% 82% 8BJava% E5% 85% A5% E9% 96% 80-% E7% AC% AC2% E7% 89% 88-% E3% 82% B9 % E3% 83% 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E4% B8% AD% E5% B1% B1-% E6% B8% 85% E5% 96% AC / dp / 484433638X / ref = zg_bs_515820_1? _Encoding = UTF8 & psc = 1 & refRID = JGCC33P1VGV3V5VKQGQR)
Recommended Posts