Static Plugin Instance

Discussion in 'Spigot Plugin Development' started by MnMaxon, May 25, 2015.

  1. When I make a plugin, I always have the code:
    Code (Text):
    public static Main plugin;

    public void onEnable(){
    plugin=this;
    }
    And then I would refer back to it with Main.plugin
    For some reason, a lot of people do not make their plugin's instance static, but I think it's a lot easier. Are there any downsides to having it be static?
     
  2. No there arent. Your server is a closed system where only the plugins you run can harm something. You can make everything you want accesible without restrictions.

    The only reason to dont make a class that has only one object in a system static is for security.
     
  3. While Static has it's pro's and con's, for the most part, you don't need/should't use it unless it's necessary. Apply static only when necessary, especially if you are going to post code here, because lots of people here think it's evil because they read it was bad once from some random guy. But in general, unless you are fluent in it's use, it's better to not use it. IMO.

    It has it's applications, but not for what you posted.
     
    • Agree Agree x 1
  4. When would use static then? The only disadvantage of static is that your object never gets garbage collected (and no object that are in the static object). His example is the perfect example where to use static.
     
  5. I've used static maybe 4 times in developing plugins for Minecraft. I didn't run across a need for it in my work often. But in other work I used static mostly for calling functions without creating the instance (usually it was for a class that required it). I'm not arguing that he can't use it.
    Side note - I don't program much but I had to learn it's applications.
     
    • Friendly Friendly x 1
  6. Thanks for the response. I assumed the plugin instance wouldn't be garbage collected either way because Bukkit needs to keep the plugin, but I'm not completely sure how garbage collection works. Is this assumption right?
     
  7. Yeah. Garbage collection deletes everything that is not being pointed at from somewhere. When you reload your server and the variable gets assigned a new value the old plugin instance will get garbage collected, once the garbage collector runs its next tick.
     
    • Agree Agree x 1
  8. Like are0planes said, there's no real reason to use a static like that unless it's really needed (usually never).
     
    • Agree Agree x 2
  9. Statics do have their uses. For example, if you wanted (for some reason) to have "String version = "1.0";" somewhere, and other classes are accessing it, I'd use static. There's no need to pass an instance of the containing class just so the other class can access a single variable. Just don't abuse them if at all possible.
     
  10. It's not "BAD" or "Evil" you just need to understand what it's uses are for, and when it is appropriate to use it.
     
    • Agree Agree x 4
    • Informative Informative x 1
  11. static objects may be collected, there just isn't a guarantee.

    Essentially, objects are collected when there are no longer any strong references to the object in memory.

    The real problem with what you posted earlier is that you assigned your plugin instance to a variable which is both public and static. You should make it private static, and have a static accessor method.
     
    • Agree Agree x 1
  12. Thank you for the response, and I'm sorry if this sounds nooby, but why would this be better than making it public and static. The only reason I could think of would be so I/other plugins don't accidentally change it, but I can't think of why that would happen.
     
  13. You're spot on. A key idea when designing your classes is liskov's substitution principle (in simple terms, this involves "programming to an interface"), and to expose as little of your class's members as possible. By default, all fields should be private and final, unless circumstances require otherwise (e.g. not final if you need to change it later, non-private such as public if it's a constant or protected if required by a subclass).
     
    • Like Like x 3
    • Agree Agree x 1