-3
class A {

    void methodA {
        B b= new B();
        b.methodB();
        //point 1
    }

    private class B {
        void methodB(){
            //point 2
        }
    }
}

if I use a system out to print this.toString() at both point 1 and point 2, it gives the same value. Can anyone please tell me why ? Shouldn't those two give different values ?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Prasad Weera
  • 1,223
  • 3
  • 13
  • 25
  • 3
    `this` *always* refers to the object/instance the method was invoked upon. So at "point 1" `this` will evaluate to an instance of class A and at "point 2" `this` will be an instance of class B (in particular, the one created with `new B()` above). If `this.toString()` gives the same value then perhaps it "generates the same string"? –  Dec 20 '12 at 07:00
  • possible duplicate of [What is the meaning of "this" in Java?](http://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java) – Stephen C Dec 20 '12 at 07:06
  • 3
    Your question title is misleading, given your further description. It's also hard to diagnose what your ***real*** problem is when you illustrate it with uncompilable code. – Perception Dec 20 '12 at 07:07

4 Answers4

2

They are different, try this:

public class Test {

    public static void main(String[] args) {
        new A().methodA();
    }
}

class A {

    void methodA() {
        B b= new B();
        b.methodB();
        //point 1
        System.out.println(this.toString());
    }

    private class B {

        void methodB(){
            //point 2
            System.out.println(this.toString());
        }
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
1

if I use a system out to print this.toString() at both point 1 and point 2, it gives the same value.

I am not agree with you.

this always refers to current object reference in java.

void methodA() {
    B b = new B();
    b.methodB();
    // point 1
    System.out.println(this.toString());
}

private class B {

    void methodB() {
        // point 2
        System.out.println(this.toString());
    }
}

Result -
Sample$B@9304b1
Sample@190d11

See from the inner class method it prints Sample$B@9304b1 where Sample is base class and B is inner class and hashcode. Where class method it prints class name with @ and hashcode.

Both hashcode is different so both object is different. so this refers different object instance.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

The simple answer is that point 1 and point 2 print DIFFERENT things ... unless you've overloaded the toString() methods:

Your program:

class A {

    void methodA() {
        B b= new B();
        b.methodB();
        System.err.println("point 1 - " + this.toString());
    }

    private class B {
        void methodB(){
            System.err.println("point 2 - " + this.toString());
        }
    }

    public static void main(String[] args) {
        new A().methodA();
    }
}

Prints this when I run it.

point 2 - A$B@58c3d9ac
point 1 - A@2207d8bb

In fact, the output clearly tells us that this means different things at points 1 and 2. At point 1, this refers to an instance of A. At point 2, this refers to an instance of A.B.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Well, It's the behavior of the toString() method.You are accessing this keyword which refers to the current instance of the class.
as visal_aim posted answer in such case at point1 this keyword will be referring to instance of Class A and at point 2 it will be referring to instance of class B.
You are getting same answer because at both the point this is type of object and that will return the same string. (Untill you have override the toString() method of base Object Class).

Hiren Desai
  • 941
  • 1
  • 9
  • 33