I would like to ask about Java instances with using or without using this keyword in below example:
I have main class of my project and then I pass it to another class like this
private final MainClass mainClass;
public AnotherClass(MainClass mainClass, itemFromMainClass)
{
this.mainClass = mainClass;
//i'm working with it again here
mainClass.ask(itemFromMainClass);
//what's the difference between accessing it via this or without this?
this.mainClass.ask(itemFromMainClass);
}
VS
public AnotherClass(MainClass mainClass, itemFromMainClass)
{
mainClass.ask(itemFromMainClass);
}
Should the itemFromMainClass also have variable defined and set on pass?
What's the exact difference accessing it with this (with adding private global variable to the class) or without this (without adding private global variable to the class)?
What impact could it have while scheduling things to repeat like every 10 seconds with this or without this?
public class AnotherClass {
private final MainClass mainClass;
private final BukkitScheduler bukkitScheduler;
public AnotherClass(MainClass mainClass, BukkitScheduler bukkitScheduler) {
this.mainClass = mainClass;
this.bukkitScheduler = bukkitScheduler;
this.bukkitScheduler.scheduleSyncRepeatingTask(this.mainClass, () -> {
doSomething(this.mainClass.getSomething());
}, 20L, 20L);
}
}
vs
public class AnotherClass {
public AnotherClass(MainClass mainClass, BukkitScheduler bukkitScheduler) {
bukkitScheduler.scheduleSyncRepeatingTask(mainClass, () -> {
doSomething(mainClass.getSomething());
}, 20L, 20L);
}
}