
class Test { void meth1() { System.out.println("meth1()"); meth2(); }
static void meth2()
{
System.out.println("meth2()");
}
public static void main(String[] args)
{
Test t=new Test();
t.meth1();
}
}
query: In the above program in meth1() the static method method2() is called. How meth2() is called?with object reference((t)(this))) or with class name(Test)? When i tried to print this (System.out.println(this);) inside meth2() it is giving error non static variable this can't be referenced from static context.
what i know : To call a non static method from a non static method no need of any object reference.we can directly call. The calling method uses this and calls.
In case of calling static method from static , we can directly
call(here class name will be used) or we can use object reference.
when calling static method from non static method,there are again
two chances only,calling with object reference(this) or class name.
If it uses current object( this ) it should allow to print this inside that class na?

Hi Friend,
Try the following code:
class Test {
void meth1() {
System.out.println("meth1()");
meth2();
}
static void meth2()
{
System.out.println("meth2()");
}
public static void main(String[] args)
{
Test t=new Test();
t.meth1();
meth2();
}
}
Thanks