Discussion in 'Spigot Plugin Development' started by yPedx, Jun 26, 2018.

  1. I've been wondering about something for a long time now..
    Is this;
    Code (Text):
    public class Main extends JavaPlugin {

    public static Main instance;

    public void onEnable() {
    instance = this;
    considered bad practice? (also using "Main.instance" in other classes)
    I'm trying to learn what static abuse is, and how to avoid it, and I'm wondering if this is a part of it.
  2. You should not name a class 'Main' as it will not be the main class in the JVM. It should be the name of the plugin.

    And no, having a static instance with a getter is the usual practice.
  3. foncused

    Moderator Patron

    This (singleton pattern) is fine, but make the instance private and add a public getter (Main#getInstance). @Sweepyoface is correct to say it is common practice, at least in plugin development. I personally prefer the dependency injection pattern to reduce coupling.
    • Agree Agree x 1
  4. FrostedSnowman

    Resource Staff

    no no! that's breaking OOP.

    pass around instances through dependency injection!
  5. Another OOP warrior I see. Singleton instances are common practices in Java and there’s no real reason as far as I know that one couldn’t use them instead of dependency injection.

    Though for the OP, if you’d like to satisfy everyone on these forums, pass an instance of your main class into a constructor for any classes that need to use it’s methods so when you instantiate it you can access your main class basically identically as to if you were to use a singleton.
  6. FrostedSnowman

    Resource Staff

    and be cool
  7. Let’s not forget that <3
  8. Really though it's god awful ugly having every single thing that doesn't need a constructor have one just to inject your JavaPlugin. There's no real advantage to not just using singleton pattern with that nice and easy static getter.
  9. FrostedSnowman

    Resource Staff

    actually there are many downsides & bad practices to this.
    • tight coupling
    • grants a universal access to the object, which just shouldn't be done. we should be creating objects and passing their instances.
    • breaks apart abstraction & doesn't allow you to provide subclasses
    • & they're not easy to parallelize in the case of a mutable state
    • Agree Agree x 1
  10. There is nothing wrong with doing that, especially if you want to piss off elitists ^_^

    In fact, JavaPlugin already has a static method you can use from other classes.
    Code (Text):
    MyPlugin plugin = MyPlugin.getPlugin(MyPlugin.class);
    If youre interested, I kinda explain a bit about static here:

    If you want to know more details/specifics just Google around. Lots of good info about it. ;)
    • Agree Agree x 1
  11. I never name my main class Main, It was just for the purpose of this thread.

    Thank you, I'll be sure to read it.
  12. The war started (I personally prefer a dependency injection)
  13. Choco


    The Bukkit class is a utility wrapper for the Server implementation, a class intended on bridging two projects, API and implementation. This is one of the very few ways to do so. Completely unrelated to singleton patterns. It is a finalized class with a private constructor indicating that it is a utility class. Utility classes != Singletons.
    • Agree Agree x 1
    • Informative Informative x 1
  14. FrostedSnowman

    Resource Staff

    as said perfectly by @2008Choco, it's a utility. the concept isn't to avoid static overall, but know when it's proper to be used. imagine if we had to do

    Code (Java):
    Math math = new Math();
    utilities are perfectly fine for having static methods that don't specifically reference anything
  15. It's okay to use the static keyword here and there, but if your project becomes a pot of spaghetti, then you're doing something wrong in an OOP language.

    I make static fields when it makes sense for a field to be shared across multiple instances of an object, such as constants. I make static methods for utilities, because it would just make life harder to instantiate something as basic as a math function whenever I want to use it.