Provide that 'this' variable stores current object reference.

0


/* 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

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts