Jammer Posted November 19, 2017 Share Posted November 19, 2017 So I'm trying to add a new task to an ArrayList or rather create a new task. tasks.add(new DropTask(this)); What I don't get is what the keyword this does here. I've only seen "this" used in methods to differenciate between the local variable and the instance variable. Might be worth adding that the constructor in the DropTask class looks like this: public DropTask(MethodProvider api) { super(api); } Quote Link to comment Share on other sites More sharing options...
Explv Posted November 19, 2017 Share Posted November 19, 2017 (edited) 13 minutes ago, Jammer said: So I'm trying to add a new task to an ArrayList or rather create a new task. tasks.add(new DropTask(this)); What I don't get is what the keyword this does here. I've only seen "this" used in methods to differenciate between the local variable and the instance variable. Might be worth adding that the constructor in the DropTask class looks like this: public DropTask(MethodProvider api) { super(api); } Consider reading this https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html Essentially all it is doing is passing a reference to the current instance (which is of type MethodProvider) as a parameter to the constructor of DropTask An alternative method, instead of passing around a instance of MethodProvider to access the API, would be to just make your other classes extend MethodProvider, and then exchangeContext: public class SomeScript extends Script { SomeOtherClass someOtherClass; @Override public void onStart() { someOtherClass = new SomeOtherClass(); someOtherClass.exchangeContext(getBot()); } } class SomeOtherClass extends MethodProvider { } Edited November 19, 2017 by Explv 3 Quote Link to comment Share on other sites More sharing options...
dreameo Posted November 19, 2017 Share Posted November 19, 2017 If you're using just 'this', then it's referencing the instance of the class. class Hi{ Hi thisCurrentObject = this; Hi(){} } Other times of using this is for ambiguity between local params and params taken from a method,constructor... w.e 1 Quote Link to comment Share on other sites More sharing options...