What should and shouldn't I use a static plugin instance for?

Discussion in 'Plugin Development' started by skipperguy12, Jun 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    skipperguy12

    Hello! So, in some of my more recent plugins, I've been using a static instance of my plugin rather than making the constructor take in the plugin for small things like schedulers, an excerpt from my Database plugin:
    Code:
    public class Database extends JavaPlugin {
        public static Database instance;
     
        public Database() {
            instance = this;
        }
     
        public void onEnable(){}
     
     
        public void onDisable() {
            instance = null;
        }
    }
    
    So, this works great for things like schedulers, I can just do something like this:
    Code:
                        Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(Database.get(), new Runnable() {
     
     
                            public void run() {
     
                        }, 1);
    And that works fine. Now, I'm wondering, should I be using this to make a static reference to non static fields ?
     
  2. Offline

    blablubbabc

    The only problems I sometimes have with static variables is when the plugin is reloaded or the jar is changed while it is running and then reloaded.
     
  3. Offline

    skipperguy12

    blablubbabc
    That doesn't answer my question, I already disable the instance when the plugin disables.
     
  4. Offline

    blablubbabc

    Me too. Still experience this problem sometimes, that it throws a "NoClassDefFound" when I call a static method on that class after the jar has changed and/or the plugin/server was reloaded. Edit: hm, could probably be caused by the class not yet being loaded at the time the jar was changed.

    I am also using static class references and methods. I just wanted to add my experience with it that under certain circumstances (which probably can be avoided) something can act weird.
     
  5. Offline

    skipperguy12

    blablubbabc
    But I'm not having any problems. My question was not how to stop the problem, nor did it mention that I had any problems with my current code, what I am asking is what should and should I be using this static instance for? Is it okay to make a static reference to a non static field using this?
     
Thread Status:
Not open for further replies.

Share This Page