Is it possible to get an object that invoked static method in this method?
I have this code:
class A{
static void foo(){
}
}
A a = new A();
a.foo();
Can I get instance a in method foo() ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Firstly, your code isn’t good as a programmer.
It is because static methods are class-level methods and should be called without any instance of class.
Recommended approach :
Nope, you can’t. Because foo() is declared as static. So you can’t use this inside that method, since this contains a reference to the object that invoked the method.