-1

I use static objects in order to use JavaPlugin in another class , but I can't understand why instance = this; should be written inside onEnable().

Why can't it be written when we declare private static Main instance;?

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    private static Main instance;

    @Override

    public void onEnable(){

        instance = this;

        saveDefaultConfig();

        Bukkit.getPluginManager().registerEvents(new Event() , this);

    }
    public static Main getInstance() {

        return instance;
    }

}
Lin898
  • 1
  • 2
  • `instance` is `static`. `this` doesn't make sense and doesn't even exist in a static context. So you wouldn't be able to assign it `this` at the declaration point because `this` wouldn't exist. Instead you do that as soon as an instance is enabled by a call to `onEnable`. By the way, this looks like a lopsided [singleton](https://en.wikipedia.org/wiki/Singleton_pattern). – Federico klez Culloca Aug 09 '22 at 08:04
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 09 '22 at 12:21

2 Answers2

1

I would use a constructor.

Second class code:

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class Event implements Listener {

    Main plugin;

    public Event(Main plugin) {
        this.plugin = plugin;
    }

    //now you can use plugin as instance.
    plugin.getLogger().info("info");

}

Main class:

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(new Event(this) , this);
    }

}
Rogue
  • 11,105
  • 5
  • 45
  • 71
Timas_bro
  • 33
  • 5
  • Indeed, using dependency injection in this way is strongly encouraged for bukkit plugins, as keeping a singleton will have repercussions with `/reload` and the plugin's classloader. Note this is a fairly old question, though, so this answer's for google I suppose. – Rogue Aug 09 '23 at 15:14
  • Yeah but the method that was first asked about is the most used and it works. Plus you’re not even supposed to use /reload anyways. Instead I always use a plugin manager which is much faster. Why does it really matter if it‘s a singleton and that it just works? – Anston Sorensen Aug 09 '23 at 19:22
  • @AnstonSorensen there's two separate problems really: the internal ones to Bukkit mentioned before, and that singletons tend to be a software antipattern. Plugins are usually fairly small, so the impact of being an antipattern isn't _major_, but on larger projects it makes it much more difficult to maintain and improve the code long-term. On Bukkit's side, the impact can present as having multiple singletons running simultaneously. Additionally, while `/reload` may be discouraged (because of this problem!), it is still widely used and supported. – Rogue Aug 10 '23 at 12:33
0

static means that you don't need an instance (like new Main()) of a class to access a field or method (classes can be static as well).

e.g. with a static method you can do

Main.method();

instead of

new Main().method();

this references a class instance. So when you call a static method there is no this reference as there is no class instance.


So from my understanding what's happening here is that you plugin framework creates a new instance and calls the method onEnable() like: new Main().onEnable(). As onEnable() is not static you can access this and assign it to the static variable. So afterwards you can access that static instance variable from the static context of your getInstance() method.

asbachb
  • 543
  • 3
  • 17
  • So , It's means that `instance = this;` can get instance from `onEnable()` and this instance is extends JavaPlugin ? – Lin898 Aug 09 '22 at 08:14
  • But Why can't it be written when we declare `private static Main instance;` ? – Lin898 Aug 09 '22 at 08:15
  • `private static Main instance = ...` would be "evaluated" when the class is loaded by the ClassLoader (this is done automatically when you try to access the class somehow). During this phase there is no instance available and therefore no `this` reference. – asbachb Aug 09 '22 at 08:20
  • My idea is that `instance = this;` written under `private static Main instance;` , that instance can extends `Main` and it exteands `JavaPlugin` – Lin898 Aug 09 '22 at 08:21
  • Yeah I know it , thanks everybody. – Lin898 Aug 09 '22 at 08:32