Instance Main Class

Discussion in 'Spigot Plugin Development' started by Torciv, Apr 24, 2017.

Thread Status:
Not open for further replies.
  1. Today I was in front of some of my plugin codes and I really wanted to see what I could improve about my code.

    Taking a look at first sight to my code from Main Class I saw that I'm really used to using Static instance:

    Code (Text):

    public class Main {

        private static Main instance;
        private OtherClass oc;

        public Main() {
            instance = this;
            oc = new OtherClass();
         
        }

        public static Main getInstance() {
            return instance;
        }

    }
     
    This means that whenever I want to use Main instance into another class I would do this:

    Code (Text):
    public class OtherClass{

        private Main plugin;
        private OtherSupport os;

        public OtherClass() {
            plugin= Main.getInstance();
            os = new OtherSupport();
        }

    }
    Nevertheless I have been searching around and I really know that using static methods isn't really a good way of programming.

    So, l decided to see what other people do.
    And most of them are using the same sytle as me... but my question would be... Is it that right? Or are we used to a non-correct way of programming?

    Another method of doing this would be passing instance in constructor/method:

    Code (Text):
    public class Main{

        private OtherSupport os;

        public Main() {
            os = new OtherSupport(this);
        }

    }
    Code (Text):
    public class OtherSupport{
     
        private Main instance;
        private Utils utils;

        public OtherSupport(Main plugin) {
           instance = plugin;
           utils = new Utils(plugin);
        }

    }
    Doing this would be more convenient but this means that I would have to pass every time the instance for every new class i'm using Main instance class.



    ===================================================

    Thanks a lot! I would like to know what do you think about this.
     
    #1 Torciv, Apr 24, 2017
    Last edited: Apr 24, 2017
  2. As far as I know, there's no real 'Correct' way of doing it, I personally pass an instance around, but I keep the static method of getting it for occasional use. If you find one way more suitable and comfortable for you, use that.
     
  3. WAS

    WAS

    There was "instances" (ha) where obtaining through static methods is the only sane method do to extension, and other stuff. For instance your main class is already instantiated by Spigot and should not be again, thus the real only way to get it is to pass the instance with new classes or a static getInstance method.
     
  4. There really isn't a proper one, and I'd like to comment on something you said:
    Static is a wonderful thing if used correctly. There is no problem with it whatsoever, just that new programmers think it's some sort of "oh, i need access to this. lets static it" kind of a drill, which it isn't.
     
    • Agree Agree x 1
  5. WAS

    WAS

    Guilty. I was not used to how... Verbose Java was, even as far as how verbose goes. So when I am definitely guilty of "dammit I need this right here and now, no fuss. Ok ill make it static" lol
     
  6. Mas

    Mas

    Just wondering, why do you create an instance of your Utils class?
    Utility methods are generally static (and should be) so there is no need to instantiate the class.
     
  7. WAS

    WAS

    Generally this is the case though I have seen plenty that do not. Especially if they use they are custom to a plugin/spigot API
     
  8. Mas

    Mas

    What do you mean "they are custom to a plugin/spigot API"?
    I can't really think of a situation where you'd need to create an instance of a Utility class.
     
  9. WAS

    WAS

    I literally explain a very solid reason, your utility uses the Spigot API, like a synchronous task. Theres lots of reasons and really again, up to the end user. If you are "utilizing" (ha) a utils class a whole bunch to its also easier on the compiler to not make static calls to a utility class all over your project(s).
     
    • Agree Agree x 1
  10. It was an example and yes I need it as @WAS explained.
     
  11. Choco

    Moderator

    I think you're confusing utility classes and APIs. Bukkit is an API and it's based around instances and polymorphism / inheritance. Very little of its methods are static and they're all in a very specific hierarchy. As for utility classes, most (if not all) methods should be static. Take for example Bukkit (the class), Math (Java's mathematics utility class), or GlStateManager in Minecraft's client source code to assist with various OpenGL methods in LWJGL. Granted, utility classes can be part of an API (Again, I take the example of the Bukkit class), but in general, they are used for static methods to ease programming in other portions of the code - i.e. Bukkit wraps methods from the Server to provide static access without an instance of the server (it holds an instance for itself).

    I can also confidently say that the compiler is not stressed by invoking static methods compared to instance-methods. In fact, due to static methods not being polymorphic, the JVM makes less decisions in order to access the method. Nevertheless, when it comes to static, performance isn't what should be taken into consideration. It's whether it should be used or not and whether it is acceptable in the given situation

    @OP Many people argue that the "main" class of a plugin is considered a singleton because Bukkit maintains an instance of your plugin and restricts you from creating another one. Often times a static method to retrieve an instance is useful (i.e. for situations where passing an instance through a constructor is difficult or simply does not suit the structure of your code, such as in an API class). Though dependency injection is recommended where possible such as with listener or command classes, if you really cannot pass an instance in a sane manor, then a static method is fine. In fact, you should take a look at the JavaPlugin class which already provides you with a method to statically retrieve an instance of your class
    Code (Java):
    YourMainClass instance = JavaPlugin.getPlugin(YourMainClass.class);
     
    #11 Choco, Apr 25, 2017
    Last edited: Apr 25, 2017
    • Useful Useful x 3
    • Like Like x 1
  12. WAS

    WAS

    Wtf? I think you just misunderstand a lot. I am talking about a custom util class that utilizes the Spigot API, such a synchronous tasks or manipulation, and you should not be calling statically from just anywhere without being synchronized throughout the project. Its called an example where a static method from utility class would not be appropriate. My blast radius land management is currently static over dev build which is a instance, and that alone sped up execution of a blast zone. In general you could build a exclusive Bukkit Utility class that handles a lot of redundant execution of things you do a lot and have it be instantiated. There is no mandate on how to make a utility class, it's the fact most are just string and primitives/math manipulation

    For instance i am working on helping a friend with a android app and one of the util classes is not static because of how it it interacts. Than he has normal plain text and number formatting util clases which as static.
     
    #12 WAS, Apr 25, 2017
    Last edited: Apr 25, 2017
Thread Status:
Not open for further replies.