So let's say I have this code for a class:
public class A { static int myInt; static void setMyInt(int myInt) { // ... } } I don't know why you would have a setter in a static method, but this is just an example. Normally for a non-static situation, I would do something like this:
public class A { int myInt; void setMyInt(int myInt) { this.myInt = myInt; } } Using this accesses the field instead of the parameter. However, in the static method, I can't access this since it is a non-static field. How do I go about accessing the field from a static method?
this. You cannot access a non-static field from a static method, unless you first create an instance and access that instance's field. You can return the instance from the static method, so that the caller can see the set instance field.