Pages

Pages

Provide that 'this' variable stores current object reference.



/* Provide that 'this' variable stores current object reference.
print this variable in constructor and non-static method. In both placess same output is printed. i.e., classname@hashcode   */

// prog: ThisNSM.java

class A
{
A(){
System.out.println("In Constructor this:"+this);
}
void m1()
{
System.out.println("In NSM this:"+this);
}
}


class ThisNSM
{
public static void main(String[] args)
{
A a=new A();
System.out.println("In main a:"+a);
a.m1();
}
}

Compilation:
G:\netlojava> javac ThisNSM.java

Execution:
G:\netlojava> java ThisNSM

Out Put:

In Constructor this:A@13f5d07
In main a:A@13f5d07
In NSM this:A@13f5d07

No comments:

Post a Comment