[For beginners] You will definitely understand in 10 minutes! What is JavaBeans?

There are JavaBeans that control the storage part of Java. I will explain what this is like so that Java beginners can get a feel for it.

What are JavaBeans?

Think of JavaBeans as ** banks **.

Imprint ** JavaBeans = Bank ** in your brain without thinking about anything extra. After imprinting, proceed to the next item.

What do you mean?

↓ Imagine a general membership registration form like this. https://www.uniqlo.com/jp/member/registry

Since the purpose is to register as a member when the page is opened, we will quietly enter personal information. I think you should enter the email as [email protected], the password as hogeword, the gender as a man, and so on. I think that the screen will change to the confirmation screen when the input is completed, but then ** Where should the personal information entered so far be stored once? ** is the problem. JavaBeans Bank ** is the bank that temporarily stores personal information at this time.

JavaBeans Bank will store the entered information in the pre-specified storage method. This is called ** class ** or ** bean definition **. Programmatically, I will write like this ↓

public class Kaiin{

String name; //name
int age; //age

}

This means, "In the definition of the name Kaiin, only the string type name and the integer type age can be stored."

How to use JavaBeans Bank

As with real banks like Thirty and Kami, you need to open an account when depositing data with JavaBeans Bank. At JavaBeans Bank, opening an account is called ** instantiation **. It is a guy who is commonly said to be new. Programmatically, I will write like this ↓

Kaiin kaiin = new Kaiin();

this is

Open an account according to the format Kaiin. The account name is "kaiin". (In the above example, only the name and age can be stored)

It shows that. You can use the opened account at any time within the program by saying "Use the kaiin account".

It's bad if anyone touches the safe

"Our bank is self-service! We keep the safe open so everyone is free to deposit and withdraw money!" I can't trust that bank, and it's easy to imagine a bad person stealing money. Considering these risks, JavaBeans Bank also decided to move data in and out through a specific window, rather than letting anyone touch it freely. The name of this window is ** get in charge of retrieval and set in charge of storage **. Programmatically it looks like this ↓

public class Kaiin{

private String name; //name
private int age; //age
Kaiin(){}; //constructor

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(ing age) {
    this.age = age;
}
}

You can see that the qualifier has been changed to private. This is stored in a safe and cannot be directly touched by outsiders. Data is taken in and out via set and get with public qualifiers.

In summary, the rules at JavaBeans Bank are

・ The data itself should be private and hidden in the back of the safe. ・ Please ask set and get to put in and take out data, not by the user.

It means that. This is called ** encapsulation ** in Java terminology.

Try to get data in and out

Now that you know the specifications of JavaBeans Bank, I would like to write a concrete program.

public class CallName {
    public static void main(String[] args) {
    
    Meibo meibo = new Meibo(); //①
    meibo.setName("Kyoko Fukada"); //②
    system.out.println(meibo.getName()); //③ ④
    }
}

public class Meibo{
    
    private String name; //name
    Meibo(){}; //constructor

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

When you run this program, the output is "Kyoko Fukada". To explain step by step

① Open an account according to the storage method called Meibo class. The account name is "meibo". ② Account name Store name information in meibo. The data to be stored is "Kyoko Fukada", and please work with set. ③ Withdraw your name information from your meibo account. Please do the work to get. ④ Outputs the name that get has withdrawn from the meibo account.

It will be the flow. Did you get the atmosphere somehow?

JavaBeans Bank has drawbacks

JavaBeans banks have a fatal drawback. that is

** Only one data can be deposited **

about it. Let's rewrite the above program a little.

meibo.setName("Kyoko Fukada");
meibo.setName("Kanning Takeyama");
system.out.println(meibo.getName());

The result is "Kanning Takeyama". At JavaBeans Bank, the data that comes in later is prioritized, so Kyoko Fukada disappears. If you want to keep both Kyoko Fukada and Takeyama Kanning, you will need to change the name and open multiple accounts. ** **

Meibo meibo1 = new Meibo(); 
Meibo meibo2 = new Meibo(); 
meibo1.setName("Kyoko Fukada");
meibo2.setName("Kanning Takeyama");

system.out.println(meibo1.getName())
system.out.println(meibo2.getName())

With this, we were able to output both "Kyoko Fukada" and "Kanning Takeyama".

Summary

Did you understand somehow? I think that the data flow has a lot to remember while using it, so please program a lot and remember it.

Although it is flowing lightly, encapsulation and getter / setter are important parts of ultra super in learning object orientation. It's okay to take some time, so it's a good idea to understand it carefully.

Finally, I will briefly review.

・ Open an account (new) when storing data in JavaBeans ・ Data is taken in and out via get and set (getter / setter) -JavaBeans can only hold one, so if you want to store multiple data, change the name and open multiple accounts.

Development and supplement

--This time, "only name" is stored, but "all account information" can be stored in another account. ――When you google with JavaBeans, you will see "Reusable ...", but I think you should forget it. If you pursue what you don't understand, you won't understand it. As you become able to build larger programs, the moment will come when you will understand "this is what it is", so let's leave it until that time. ――Speaking behind the scenes, when you do new, it "allocates a storage area in memory". --The bean definition file name has various expressions such as hogeBean.java and hogeForm.java depending on the framework.

Recommended Posts

[For beginners] You will definitely understand in 10 minutes! What is JavaBeans?
[For programming beginners] What is a method?
[For super super beginners] What is object orientation?
A simple analogy of Java classes, instances, and objects. .. .. [Series that makes you feel like you understand]
[For beginners] You will definitely understand in 10 minutes! What is JavaBeans?
Understand in 5 minutes !! How to use Docker
[Java] Understand in 10 minutes! Associative array and HashMap
[Java] What is JavaBeans?
What you write in C # is written in Ruby like this
What is object-oriented programming? ~ Beginners ~
[Ruby] What is `!!` used for?
What is the constructor for?
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]
[For beginners] Ruby is said to be ruby, but what about it?
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
What is a snippet in programming?
What is @Autowired in Spring boot?
What you can do with ruby ​​Quick reference teaching materials (for beginners)