Which one way is better: static or plugin?

Discussion in 'Plugin Development' started by Ibas, Jun 18, 2013.

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

    Ibas

    Hello,
    First let's call our classes. A = Main class, B = another class in same package.
    If I'm working in B and I want to access method from main's class, should I use something like this in B class:

    Code:java
    1. private MainClass plugin;
    2. public AnotherClass(MainClass plugin) {
    3. this.plugin = plugin;
    4. }


    Or just make that method static in A class and get it in static way?:

    Code:java
    1. MainClass.<Needed Method>;



    Which one should I use and why?
     
  2. Offline

    MCForger

    Ibas
    I dont really know if any certain method is "better" but I like using the first one you posted. It makes it feel organized to me.
     
  3. Offline

    ZeusAllMighty11

    You should try not to use statics whenever possible. One of the reasons I switched to grabbing my main classe's instance statically, is because I kept getting stuck in a never ending constructor loop which would crash the server and grow the server.log to such an immense size.
     
  4. Offline

    Tirelessly

    You can't ask a question like this without us knowing the specific function. Some things are better static, some are better as instance.
     
  5. Offline

    microgeek

    Why are static instances bad? I've never had issues with them.
     
  6. Offline

    Ivan

    They tend to create memory leaks when the plugin gets unloaded.
     
  7. Offline

    EvilKanoa

    I use something that is kinda a hybrid...ish. Anyways, I make a private MainClass variable inside MainClass and assign that to the instance like so: mainClass = this; and then a make a public static MainClass getInstance() {
    return this.mainClass;
    }
    Then you can use MainClass.getInstance().<Needed method>;

    Anyways, I'm on my phone, so there is probably some syntax errors in there :p

    Goodluck!!!
     
  8. Offline

    ZeusAllMighty11

    microgeek

    Not just static instances, static variables as well. Garbage Collector in Java doesn't always recycle them, so they can build up over time. Of course, over time, not in a single day unless you had thousands or so.

    Null them on disables, and you'll be fine
     
  9. Offline

    Diemex

    I have had issues with static variables in particular. On a reload of the server they stay, with their old values and everything. I prefer to pass the plugin instance to classes that require it. It makes sure that everything gets cleaned up correctly on a reload. Of course it might still be a good idea to nullify big objects nevertheless. The reload command is wonky and I just feel like using static stuff when not really necessary doesn't help make it any better.
     
  10. Offline

    bitWolfy

    Ivan That should fix the problem, no?
    Code:java
    1. @Override
    2. public void onDisable() {
    3. instance = null;
    4. }
     
  11. Offline

    Minnymin3

    TheGreenGamerHD
    But when the server is shut down all excess data is removed right? So the only buildup would be over a very large period of time or if someone spams /reload (like I tend to do while testing :p).
     
  12. Offline

    ZeusAllMighty11

    Not always
     
  13. Offline

    minoneer

    First of all: both ways work and imo none ist generally "better". It really depends on the plugin.

    If you have a small plugin, with only a few classes, the easyer and safer way would be to pass the instances to the constructor.

    For a Plugin of larger size, with many classes, an API, etc. I prefear a static method + variable. That way I don't have to hand the plugins instance through all classes. The downside is, that you have to make sure to always keep the right instance on the field, set it null on unload etc.

    Regards, minoneer
     
  14. Offline

    Ivan

    If thats the only static reference, yeah :)
     
  15. Offline

    CubieX

    Reloading a server is suicide anyway.
    Better never do this, unless it's your development server. (and even then, better do a proper restart)
    Why? Because you can't know if all of your used plugins are able to handle a reload properly.
    I bet many of them won't.
    I deactivted the /reload command on my server completely, because of this.
    ~60 Plugins WILL mess up things on reload.... :rolleyes:
     
    EvilKanoa likes this.
  16. Offline

    Minnymin3

    60 plugins ya. I build my plugins to work flawlessly on reloads mainly because I love the /reload command and hate restarting my server and one bug that happened on reload was bugging me so I fixed it and it works fine now :D
     
Thread Status:
Not open for further replies.

Share This Page