java - Explanation on "this" statement
<p>I am new to Java, really confused about the outcome of the following program, could someone please elaborate on swapping scenarios? </p>

<p>Also, could someone give me few examples with regards to <code>this</code> keyword?</p>

<pre><code>class P {
    int i;

    void test1(P p1, P p2) {

        int i = p1.i;
        p1.i = p2.i;
        p2.i = i;;
    }

    void test2(P p1) {

        int i = this.i;
        this.i = p1.i;
        p1.i = i;
    }

    public static void main(String[] args) {
        P p1 = new P();
        P p2 = new P();
        p1.i = 1;
        p2.i = 2;
        p1.test1(p1, p2);
        System.out.println(p1.i + "," + p2.i);
        p1.test(p2);
        System.out.println(p1.i + "," + p2.i);
    }
}
</code></pre>

<blockquote>
<pre><code>//Output
2.1
1.2
</code></pre>
</blockquote>

<p>Thanks.</p>
