0

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);
    }
}
  • "//what's the difference between accessing it via this or without this?" at that point none, since they reference the same object because you assigned the latter to the former. – Federico klez Culloca Dec 06 '21 at 13:13
  • The relevant difference is that in the first, you assign the value of the parameter `mainClass` to the field `mainClass`, while in the second you do not. – Mark Rotteveel Dec 06 '21 at 13:17
  • Thanks guys if I had scheduled task that would be started with the program and repeated every 10 seconds, would you recommend 1. or 2. example and why? – SQLLearnerMan Dec 06 '21 at 13:21
  • @SQLLearnerMan scheduled how? You'll need to include a lot more detail about your design. As it stands we don't have enough information to decide what method would work. – Federico klez Culloca Dec 06 '21 at 13:23
  • hi , This is about access, you called the Class method from within another class that used this method. The only difference is in access. and you create new instance of MainClass That means you have two instances – hani Dec 06 '21 at 13:26
  • I added examples to question. – SQLLearnerMan Dec 06 '21 at 13:36
  • Given the example, you don't even need a class at all. A static method with the contents of that constructor is sufficient, so no need to construct a class you don't really use. – Mark Rotteveel Dec 06 '21 at 13:41
  • Thanks and let's pretend I would need it, what would be the difference then? – SQLLearnerMan Dec 06 '21 at 13:45
  • Functionally there is no difference, technically you're populating two fields in an object, which you then don't really use. – Mark Rotteveel Dec 06 '21 at 13:46

0 Answers0