Java encapsulation and getters and setters

Encapsulation

When developing with some people, when you create a class, expose the features you want to use and hide the features you don't want to use outside the class so that others can use the class. Specifically, it restricts access to fields and methods. Use "public" to make it accessible from outside the class, and "private" to make it inaccessible. [Example]

public String name;  //Can be accessed from outside the class
private String name;  //Not accessible from outside the class

Getter

In "private" earlier, I made it impossible to access the field from outside the class. However, even "private" can be accessed from within the class. What you do is make the field "private" and define a method that just returns the value of the field in order to get the value of the field from outside the class. This is called a ** getter **. It seems that getters are generally named like "get field name". [Example] The class is Person

class Person {
public Return type get field name() {
    //Returns the value of the field
  }
}

Setter

If you set the field permissions to "private", you cannot change the value of the field from outside the class. Therefore, define a method to change the value of the field. The method of changing the value of a field is especially called a "setter". It seems that setters are generally named like "set field name". I think it's okay to remember that the standard encapsulation is "private" for fields and "public" for methods. [Example]

class Person {
public void set field name(Data type dummy argument) {
    //Set value in field
  }
}

Recommended Posts

Java encapsulation and getters and setters
Getters and setters (Java)
java (encapsulation)
[Java] Encapsulation
About Java setters and getters. <Difference from object orientation>
Java and JavaScript
XXE and Java
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
About encapsulation and inheritance
timedatectl and Java TimeZone
[Java] Branch and repeat
Java encapsulation private public
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Study Java # 2 (\ mark and operator)
[Java Bronze learning] Differences between encapsulation, data hiding, and information hiding
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.
Java methods and method overloads
java Generics T and? Difference
About Java Packages and imports
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements
Java Beginner Escape Boot Camp Part 2 Understanding Java Classes and Encapsulation
Is it possible to automatically generate Getters / Setters with Java Interface?
About Java static and non-static methods
I compared PHP and Java constructors
Differences between "beginner" Java and Kotlin
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable
Java Excel Insertion and Image Extraction
AWS SDK for Java 1.11.x and 2.x
[Java] Basic types and instruction notes
Java and first-class functions-beyond functional interfaces-
About fastqc of Biocontainers and Java
Java Primer Series (Variables and Types)
Encoding and Decoding example in Java
[Java beginner] About abstraction and interface
[Java] Loop processing and multiplication table
Java 15 implementation and VS Code preferences
OpenJDK 8 java and javac command help
Use JDBC with Java and Scala.