「Java」クラスのthisでインスタンス変数を利用するサンプル
書式
this.インスタンス変数名
使用例
package com.arkgame.study;
//クラスInfoの定義
class Info {
int nn = 20;
// メソッドの定義
public void show() {
int nn = 30;
System.out.println("ローカル変数: " + nn);
// this.変数
System.out.println("インスタンス変数: " + this.nn);
}
}
public class ThisSampleDemo {
public static void main(String[] args) {
// オブジェクトの作成
Info cft = new Info();
// 関数を呼び出す
cft.show();
}
}
実行結果
ローカル変数: 30
インスタンス変数: 20