• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does usage of this keyword in static method throws an error  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i wrote a small peace of code which is as follows



all the while i knew that static method does not allow usage of this keyword for accessing instant variables in them. Now to know the error i wrote the above code. But just wanted to understand as to y we cannot access instant variables from a static method with this keyword. though vaguely understood this, but wanted to have a clear picture.

Thanks

Krishnadhar
 
vp reddy
Greenhorn
Posts: 7
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

static method belongs to class , instead of instance/Object , we can  not use this keyword.

this refers to that instance/object.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

vp reddy,

i know all the 3 points that you have mentioned, question was y we cannot access instance variables, is it because of the points mentioned or is there any other reason. which is more precise.

Thanks
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first thing to understand is what is the meaning of saying something belongs to a class: it means anything static(methods,blocks,variable) is visible to every instance of the class i.e., all the instances are sharing its single copy(if any instance does some modification for static thing then all the other instances can view this modification).now "this" keyword means a reference to the current object but since anything static covers all the instances of a class,even those who were not yet created,that's why the term "belongs to the class" is used,the static thing cannot make a sense about what a current object is.that's why it's called that you cannot use a non-static thing in the static context.

Hope it helps!

Kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What instance variables? What if you call that static method correctly, ie ClassName.staticMethodName()? What if you have not created any instances yet? Where will the instance variables be? What if you have created multiple instances, each with different values of its instance fields? Which value will the static method access?
When you think about those questions, it shou‍ld start to become obvious that you cannot access instance members from a static method. The keywords this and super refer to the object or part of it. If you can't find instance fields, how can you find the instance? So there is a blanket prohibition against accessing instance members or instances from inside a static method, unless you have a reference to that instance.Here something() has to be a static method, but someOtherMethod doesn't have to be static. You can access it because you have a reference to a particular instance, which is chosen from a non‑static context.

I think non#x2011;static is a bad thing to say, and the error message misleads people into thinking you need to add the keyword static to things. The correct answer is usually to remove the keyword static from the method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!