0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaBeansについて

Posted at

用途

プログラムを部品化する。
プログラム間でデータの受け渡しに使う。

3つの規則

  • privateなフィールドへのgetterおよびsetterメソッドを持つ
  • publicで引数なしのコンストラクタを持つ(※)
  • java.io.Serializableを実装している

※コンストラクトを定義しなかった場合、コンパイラが引数なしのコンストラクタを自動的に生成する

Sample

UserInfo.java
package bean;

public class UserInfo implements java.io.Serializable {

    private String name;
    private int age;
    private String nationality;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getNationality() {
        return nationality;
    }

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

    public void setAge(int age) {
        this.age = age;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

}

上記Beanを使用する。

Main.java
package test;

import bean.UserInfo;

puublic class Main {
    public static void main(String[] args) {

        UserInfo userInfo = new UserInfo();

        userInfo.setName("John Smith");
        userInfo.setAge(30);
        userInfo.setNationality("United States");

        System.out.println("name :" + userInfo.getName());
        System.out.println("age :" + userInfo.getAge());
        System.out.println("nationality :" + userInfo.getNationality());
    }
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?