What is a class in Java language (3 /?)

Introduction

Good evening. Again. It's been 3 days since I started writing the article. I'll do my best today too.

Encapsulation

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.

in conclusion

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.

References

[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

What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
What is a wrapper class?
What is a Java collection?
[Java] What is class inheritance?
[Java basics] What is Class?
What is a snippet in programming?
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
What is java
What is Java <>?
What is Java
What is the main method in Java?
Creating a matrix class in Java Part 1
What is a constructor?
What is a stream
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
What is a Servlet?
[Java] What is JavaBeans?
[Java] What is ArrayList?
GetInstance () from a @Singleton class in Groovy from Java
A quick review of Java learned in class
What is Java Assertion? Summary.
What is a boolean type?
What is a Ruby module?
What is the difference between a class and a struct? ?? ??
What is a floating point?
A quick review of Java learned in class part4
What is a meaningful comment?
Write a class that can be ordered in Java
Write a class in Kotlin and call it in Java
What is the BufferedReader class?
What is a jar file?
Find a subset in Java
What is a lambda expression?
What I learned when building a server in Java
[Java] What is jaee j2ee?
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Cause of is not visible when calling a method of another class in java
What is a fa⁉ enum?
Java11: Run Java code in a single file as is
What is java escape analysis?
What is the LocalDateTime class? [Java beginner] -Date and time class-
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
[Java] A class is an OS, and an instance is a virtual computer.
Road to Java Engineer Part2 What kind of language is Java?
What is JVM (Java Virtual Machine)?
3 Implement a simple interpreter in Java
I created a PDF in Java.
How slow is a Java Scanner?
thread safe process in java language
StringBuffer and StringBuilder Class in Java
A simple sample callback in Java
[Java] What got caught in encapsulation
What is thread safe (with Java)
What is a column Boolean type?