Main class instance code!

Marcelektro

Founder, Developer
Staff member
Aug 2, 2019
146
21
18
Hey!
Today I will show you the best and the easiest way to get the instance of your Main class in Java!
This will be useful in coding Minecraft plugins.

You gonna put this code into the Main class:

Java:
public static Main plugin;

public Main() {
    if(plugin != null) {
        throw new IllegalStateException("Nani!");
    } else {
        plugin = this;
    }
}

public static Main getInstance() {
    return plugin;
}
Now, to get Instance you do Main.getInstance() and you have it :D

If your Main class is named for example "ClassName123", then replace "Main" in the code to the name of your class, and call it with it's name either!

I hope his helped!
Regards
Marcelektro-kun

Did you know I use this in marCloud's source code? :)