Why are you giving this Static variable error?

Asked

Viewed 201 times

6

Why is giving this build error and how can I fix it?

Cannot make a Static Reference to the non-static field service

Pq dá esse erro?

  • 1

    Read this: http://meta.pt.stackoverflow.com/q/5149/101

  • Since service is not static, you can not call the service by a static method.

  • 1

    Oops, corrected here haha. I’m laughing, is that I wrote but with the intention of testing to see if it worked even.

1 answer

7


Because the method is static and the variable being used is instance. There is no relationship between them. Static members have as "owner" the class. There is only one throughout the application. Instance members have as "owner" the current instance, that is, each object created from that class owns its members. You can have as many as you want. Since the owners are different, one member cannot access another owner’s member.

The solution is to take the static method. It may be that the solution is to place a static variable. I have no way of knowing what you have in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.