BohYoh.comトップページへ
Java FAQ
目次

thisとは何ですか。

 単独のthisは、インスタンスメソッドを呼び出したオブジェクトあるいは生成されたオブジェクトへの参照です。

class ABC { int a; ABC(int a) { this.a = a; // this.aすなわち自分自身のフィールドaに // パラメータとして受け取ったaを代入 } boolean equals(ABC other) { if (this == other) // このメソッドを呼び出したオブジェクトとotherは同一オブジェクト else // このメソッドを呼び出したオブジェクトとotherは別のオブジェクト } }

 メソッド呼び出し形式のthisは、他のコンストラクタを明示的に呼び出します。

class XYZ { int x; int y; XYZ next; XYZ(int x, int y, XYZ next) { this.x = x; this.y = y; this.next = next; } XYZ(int x, int y) { this(x, y, null); // ↑のコンストラクタを明示的に呼び出す } }


戻る

BohYoh.comロゴ