' P '

whatever I will forget

Java toString メソッドのOverride Tips

クラスを作れば、JavaのObjectクラスから継承したことになるので
class内ではtoStringメソッドをoverrideすることができる。

使い方

  • デバッグ用に作っとくと楽よ
  • クラス内のメンバの値をString値で返す

サンプル

public class foo {
    int i;
    int j;

    public foo(int numA, int numB) {
        this.i = numA;
        this.j = numB;
    }

    public int getI () {
        return this.i;
    }
    
    public int getJ () {
        return this.j;
    }

    @Override
    public String toString() {
        return this.i + " : " + this.j;
    }
}