Java Beginner Escape Boot Camp Part 2 Understanding Java Classes and Encapsulation

About this time

In this article, you will learn about "classes", which are the basis of programming languages based on object thinking.

What is a Java "class"?

First, before the class

"What is object-oriented ???"

Let's start with what that means.

Origin of object thinking

The most important thing in the idea of object thinking is ** "role" **.

From the user's point of view, a program works as one.

For example, a smartphone game that you often play can be done with just one icon. There are many programs running behind the scenes.

For example, in a rhythm game

--Musical information

So, there are various elements.

Of course, you can put all this into one file and proceed with the program, I think you can easily understand that such a thing would be difficult.

Object thinking aims to separate these elements by "role".

Convenient by separating roles

The main advantage of separating classes by role is that they are "easier to understand".

Suppose you divide the classes related to the progress of the game as follows.

--Music playback --Stamina management --Gacha

For example, suppose the following specification changes occur.

Changed stamina recovery speed from "1 gauge in 10 minutes" to "1 gauge in 5 minutes"

Where should I fix it in such a case?

The answer is simple, ** "stamina management" **.

In this way, by leaving all the processing related to stamina to the stamina management class, It will be very easy to understand when modifying.

The following is an example of a stamina management class.


public class stamina management{

private int stamina;

private long Most recent recovery time;

public int get stamina() {
    return this.Current stamina; 
  }
  
public void recovery check() {
    
    //Get current time with millSec
long current time= new Date().getTime();

    //10 minutes= 10 * 60(sec) * 1000(millSec)
    if (Current time-Most recent recovery time> 10 * 60 * 1000) {
      //Restores 1 stamina
      this.Current stamina++;

      //Update the latest recovery time to the current time
      this.Most recent recovery time=Current time;
    }
  }
}

Now, the point here is the part related to the numerical value of stamina.

private int stamina;

private int most recent recovery time;

This variable declared inside the class is called "field value" and refers to the variable that can be managed in the class. As a general rule, the field values declared here can be accessed from anywhere within the class **.

Especially in Java, it is common to make this field value private so that the value cannot be changed directly from the outside.

Getter

Now, in this class, we have prepared the following methods to get the current stamina.

public int get stamina() {
    return this.Current stamina; 
  }

A method that aims to get a feed value in this way is called a "getter".

Only methods declared as public can be called from outside the class. By preparing a getter,

--You can get the current stamina just by calling get stamina () --You will not be able to rewrite your stamina directly

You can specify that.

In this way, you can suppress unintended movements by intentionally limiting the operations on the fields of the class itself.

Further specification change of specification change

Now, let's recall the specification change earlier.

Changed stamina recovery speed from "1 gauge in 10 minutes" to "1 gauge in 5 minutes"

To achieve this, the following parts should be modified.

    if (Current time-Most recent recovery time> 10 * 60 * 1000) {

Well, when I thought about fixing it in 5 minutes, I was told something like this.

Basically 10 minutes, at the time of the event 1 gauge per minute or 1 gauge in 5 minutes

Well, in this case, just rewriting is not enough.

Setter

It can't be helped, so let's change this recovery span to a field value so that it can be rewritten from the outside.


public class stamina management{

private int stamina;

private long Most recent recovery time;

private long recovery span;

public int get stamina() {
    return this.Current stamina; 
  }

public void set recovery span(int recovery fraction) {
    this.Recovery span=Recovery fraction* 60 * 1000;
  }
  
public void recovery check() {
    
    //Get current time with millSec
long current time= new Date().getTime();

    //10 minutes= 10 * 60(sec) * 1000(millSec)
    if (Current time-Most recent recovery time> 10 * 60 * 1000) {
      //Restores 1 stamina
      this.Current stamina++;

      //Update the latest recovery time to the current time
      this.Most recent recovery time=Current time;
    }
  }
}

By the way, the following methods are introduced here.

public void set recovery span(int recovery fraction) {
    this.Recovery span=Recovery fraction* 60 * 1000;
  }

If you want to reduce the stamina recovery time due to an event etc., you can change the recovery time by calling this method.

A method that aims to rewrite field values in this way is called a "setter".

Encapsulation

In this way, by restricting field access using Getters and Setters, You can prevent unexpected movements and calls.

This is called ** "encapsulation" **.

What if it was a "world line without encapsulation"?

What would happen if we lived on a world line where encapsulation did not exist?

If all the fields can be easily accessed from the outside, The following source code will be embedded everywhere on the game screen.

while(true) {
  //Run every 10 minutes
  Thread.sleep(10 * 60 * 1000);  
Stamina management.stamina++;
}

If this works multiple times, you may suddenly recover 2 stamina, or it may not recover even after 10 minutes.

If this is encapsulated ...?

What if this was well encapsulated?

while(true) {
  Thread.sleep(1000);  
Stamina recovery.Recovery check();
}

In this case, the recovery check will not make any extra recovery even if it is called continuously.

By encapsulating stamina in this way, It will be a beautiful program without creating strange bugs.

Purpose of encapsulation

Encapsulation clarifies the categories managed by the class itself, The aim is to make the call easier to understand.

This is an object-oriented program magic ing

For example, the role of this stamina management class is to "manage stamina", so I don't want to leave the behavior of increasing or decreasing stamina to other classes.

To achieve this, prohibit direct access to stamina information from the outside, ** I just ask "Hey management class, what is your current stamina?" **.

The basis of encapsulation is "make external references simpler".

Even if you hide the process or state Code that is complicated to call or that doesn't work well unless you call it in a certain order is not very good code.

at the end

This time I explained about encapsulation. By mastering encapsulation, you can reduce the so-called "fucking code" that creates many bugs.

Not limited to Java, it is very important for object thinking programming, so Please wear it properly.

By the way, most of the common Getters and Setters only assign and return values, so The usage of Setter introduced this time can be said to be a very special pattern.

Please note that in some cases it may be prohibited to actually process Getters and Setters.

Recommended Posts

Java Beginner Escape Boot Camp Part 2 Understanding Java Classes and Encapsulation
Java beginner escape boot camp Part 1 Java class structure and how to write
java (classes and instances)
[Java] Generics classes and generics methods
Java and Iterator Part 1 External Iterator
Apache Hadoop and Java 9 (Part 1)
Java abstract methods and classes
Java encapsulation and getters and setters
Differences between "beginner" Java and Kotlin
[Java beginner] About abstraction and interface
[Java beginner] == operator and equals method
Understanding equals and hashCode in Java
Java generics (defines classes and methods)
Java programming (classes and instances, main methods)
JAVA learning history abstract classes and methods