Solved What is good coding to get the instance of the main class

Discussion in 'Spigot Plugin Development' started by PoutineQc, May 17, 2016.

  1. I am developing plugins for a few months now but I always stumble on the same problem in almost every projects I do. It's how to get the instance of the main plugin class (the one extending JavaPlugin) from other classes (i.e.; for Runnables) for classes that will be instantiated many times. I did that in various ways in the past and I was wondering what is the the best way to do it in general to have the best Java coding practices.
    Here are the main ways I use:
    Code (Text):
    public class Plugin extends JavaPlugin {
        public static final Plugin plugin;
     
        @Override
        public void onEnable() {
            Plugin.plugin = this;
        }
    }

    public class SomeClass {
        private static final Plugin plugin;
     
        static {
            SomeClass.plugin = Plugin.plugin;
        }
    }
    Code (Text):
    public class Plugin extends JavaPlugin {
        private void someMethod() {
            SomeClass.initializePlugin(this);
        }
    }

    public class SomeClass {
        private static Plugin plugin;
     
        public static void initializePlugin(Plugin plugin) {
            SomeClass.plugin = plugin;
        }
    }
    Code (Text):


    public class SomeClass {
        private final Plugin plugin;
     
        public SomeClass(Plugin plugin) {
            this.plugin = plugin;
        }
    }
    So there you go, I don't know which one is considerate the best code.
    Maybe what would be best is not even in these choices.
    Remember that "SomeClass" will have a big amount of instances.
    Thank you
     
    #1 PoutineQc, May 17, 2016
    Last edited: May 17, 2016
  2. Store your main class as an object and initialize it on enable, access it statically.
     
  3. I usually make something like this:

    Code (Text):
    private static (main class name) instance;

    public void onEnable() {
             instance = this;
    }

    public static (main class name) getInstance() {
            return instance;
    }
    Make sure this is in the main class. Whenever you want to access the main class, do (main class name).getInstance();. This is the best way that I know of, and that I use often.
     
  4. Is it not a bad idea to use static fields, even so for something as important as your main class?
    I'm being the devil's advocate here, I do not disagree with you guys.
     
  5. Spigot includes a method to do this for you:
    Code (Text):
    JavaPlugin.getPlugin(*Your main class name*.class);
     
  6. Yeah that's fine. I always use this class I made and instead of extending JavaPlugin you extend SpigotPlugin. It creates an instance of your main class automatically.
     
  7. Personally, I've always stuck with the method you used in the first message, and it's never failed me. I don't think I'll ever change to be honest with you. :p

    Code (Text):
    private Plugin plugin;

    public ClassName(Plugin plugin) {

            this.plugin = plugin;

        }
    - Cervinakuy
     
  8. That should be 'BungeePlugin' since it involves Bungee channels, and would require Bungee functionality. Also:
     
  9. Alright, thanks guys :)
     
  10. Statically accessing your main class just makes it easier for the rest of your classes to reference it, and also saves you from needing to create a private field in all your other classes that use the main class. It's not a bad idea at all, it actually makes more sense since there will only ever be one instance of your main class anyways. In other words, would recommend using @Exellanix 's method (but using constructors to populate a main class field is also a viable option).

    Also, your first post is considered bad programming practice as it goes against information/data hiding. You can read up on it here. (FYI Data Encapsulation is not Data Hiding, despite what some people say)
    https://en.wikipedia.org/wiki/Information_hiding
     
    #10 m0bi, May 17, 2016
    Last edited: May 17, 2016
  11. Yeah, this was my main concern with that example :/
    Thanks for the heads-up :)
     
  12. No, it shouldn't.