0

I'm having a confusion regarding "this" keyword. Here are two code samples which are giving same output so what is the difference between them? And I do have other screen classes too. But game's starting point is SplashScreen.

public class GDXGame extends Game {
    @Override
    public void create() {
        AssetLoader.load();
        setScreen(new SplashScreen(this));
    }
    @Override
    public void dispose() {
        super.dispose();
        AssetLoader.dispose();
    }
}

And 2nd:

public class GDXGame extends Game {
    @Override
    public void create() {
        AssetLoader.load();
        setScreen(new SplashScreen());
    }
    @Override
    public void dispose() {
        super.dispose();
        AssetLoader.dispose();
    }
}

1 Answers1

1

this refers to the current instance of the current class, so the 1st example sends the current GDXGame class instance from which #create() is being called to the SplashScreen constructor, whereas the second is calling the empty constructor for SplashScreen. As to why they behave the same, or appear to, that depends on what an instance of SplashScreen does with the reference to theGDXGame

SteveR
  • 583
  • 2
  • 12