Solved First plugin.. few questions (n00b)

Discussion in 'Plugin Development' started by Pizza371, Dec 5, 2013.

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

    Pizza371

    Hello peoples of bukkit!
    Today I'm in need of your help, I am a bit confused on STATICS, and always have been.
    So, let me explain:
    I have a some instance classes, which are all fine and dandy, but I need to create classes to manage those classes (such as instance counts, getting objects, loading objects, deleting... you get the point).
    What would be the best way to do this? I've heard a lot of people saying statics are bad (over bukkit and other places on the internet). I was going to create a class with most methods static for easy access, but don't want to run into any problems down the line. How should I design these classes? What is so bad about statics (I know statics are loaded before anything else thats about it :/)?

    tl;dr:
    What's bad about using statics to manage Object classes? Or in general (when is the right time to use statics?).
    ^^

    I know this is more general than just bukkkit API, but I would be much appreciative of an answer,
    thanks!
     
  2. Offline

    beastman3226

    Statics is any reference you don't need an instance for. A list of instances would be a good use for a static. Any class that manages other objects should be static or should be a singleton (static methods are easier). Check out this, if you have more questions.
     
  3. You can use the "final" keyword when you don't want a static variable (or non static) to be altered after its declaration.

    Static variables DO NOT depend on created objects.
    e.g.
    Code:
    public class Statics_Test{
      public static int STATIC_INT=0;
      
      public static void main(String[]args){
          System.out.println(STATIC_INT);
          Statics_Test test1=new Statics_test();
          Statics_Test test2=new Statics_test();
          System.out.println(test1.STATIC_INT);
          System.out.println(test2.STATIC_INT);
          test1.STATIC_INT=5;
          System.out.println(test1.STATIC_INT);
          System.out.println(test2.STATIC_INT);
      }
    }
    
    The result should be :
    0
    0
    0
    5
    5
     
  4. Offline

    xize

    Pizza371

    statics could be bad in the way of using it people explained me that when to many classes wants to access the static field at once it could result in synchroom errors still I hope to ever find out myself what happends but I guess the jvm just crash or will have a cpu problem.

    also with static it could be a pain to track instances or objects in some cases which why people more likely would use getters and setters or just a constructor.

    on the other note static also has good things aswell when you want to globalize important methods but you need to be always very carefully by avoiding calls at the same time really when we are talking about variables or arrays.

    however I think the best way is to make setters and getters to static but don't do that with the ArrayList(i've never tried it myself since its new for me to).

    i've read somewhere that there is some trick with static
    Code:
    public static justAInstance test() {
          return something;
    }
    
    I'm not sure if it whas this but the idea is to use static as a method instead of doing it by variables and make it singleton perhaps you return everytime a new static instance I don't know, I hope somebody could explain it or can give feed back since thats a part I'm still confused in aswell.

    I hope it helps a bit;)
     
  5. it wont...

    static variables will be changed to the value the last method had assigned.



    Code:java
    1. public class Statics_Test2{
    2. public static int STATIC_INT=0;
    3.  
    4. public static void main(String[]args){
    5. System.out.println(STATIC_INT);
    6. Statics_Test test1=new Statics_test();
    7. Statics_Test test2=new Statics_test();
    8. System.out.println(test1.STATIC_INT);
    9. System.out.println(test2.STATIC_INT);
    10. test1.STATIC_INT=5;
    11. System.out.println(test1.STATIC_INT);
    12. System.out.println(test2.STATIC_INT);
    13. new Thread(){
    14. public void run(){
    15. while(true)
    16. STATIC_INT=1;
    17. }.start();
    18. new Thread(){
    19. public void run(){
    20. while(true)
    21. STATIC_INT=0;
    22. }.start();
    23. Thread.sleep(10000)
    24. System.exit(0);
    25. }
    26. }

    try it if u like
    static instances tracks instances same as non-static fields...

    en enum is a class with many static + final fields but it still works good. Material is an example of an enum. (there are much more)

    most plugins consists of 1 thread only.

    your method returns the same object. some devs like to use instances instead of making all method static to reset the values easily. take this as an example:

    Code:java
    1. public class Test{
    2. private static Test inst; //holds the instance
    3.  
    4. public int variable1=0,variable2=0,variable3=0; //assume there are 3 variables,
    5. //and their default value is 0
    6.  
    7. public Test getInstance(){
    8. if(inst==null){ //creates an new instance when null
    9. inst=new Test();
    10. }
    11. return inst;
    12. }
    13.  
    14. public void resetAllValues(){
    15. inst=new Test(); //Creates a new Test object, and the variable inst points to that object
    16. //the values are "resetted". However, the values in the object before are NOT resetted.
    17. //we get the NEW object when calling getInstance(), so the old object it omitted.
    18. }


    Code:java
    1. public class Test{
    2.  
    3. public static int variable1=0,variable2=0,variable3=0; //assume there are 3 variables,
    4. //and their default value is 0
    5.  
    6. public void resetAllValues(){
    7. variable1=0;
    8. variable2=0;
    9. variable3=0;
    10. //in this time, we need to reset all values one by one, and it is a lot of typing when you have so many fields
    11. }
     
  6. Offline

    1Rogue

    Show Spoiler



    You do not need static for nearly anything in bukkit plugins. You should pass instances of your classes instead.
     
  7. Offline

    beastman3226

    Statics are very useful in Bukkit plugins and are very useful in everything you do programming wise.

    1Rogue
    Plugin instances as a static are all good. There is nothing wrong with this so long that you set the instance to null afterwards. Statics are a very efficient way of programming (especially when you don't need a new variable for every instance of the containing object). I really wish MORE people would start using statics.

    xize
    Statics are useful, the only bad way to use statics is to have multiple things edit the variable at the same time. If you do have this happen, it should not be static.
     
  8. Offline

    1Rogue


    1) Static (in my experience) is the cause of a good majority of the problems with new programmers.


    2) There is no guarantee that static instances will be garbage collected, even if you set the instance to null.'.

    3) There is nothing wrong with multiple variables that point to the same object. Especially with objects/plugin instances, java is pass by reference in this regard, and you are simply pointing to the main plugin instance, not creating a new plugin instance for each one (read: this will not cause any degradation in performance / resource usage).

    4) How do you propose to nullify a plugin object outside of the classloader without using an external member to manage it?


    5) Statics are stored in the permanent generation of the garbage collector and are persistent throughout the run-time of the program. This points back to #1 where even when set to null there is absolutely no guarantee that it will be collected

    6) Concurrency is not a reason against static objects (it is merely a thread safety standard). If you have any variable being edited in that way it really should have synchronized access

    7) Static variables represent global state, and if I modify and instance of one class it will affect all other instances of that class with the static variable

    8) Static should not be used for general ease of use or simplifying scope / access to variables.

    I can go on for a while in this, but in general, unless you know what you are using static for, do not use it.
     
  9. Offline

    beastman3226

    1Rogue
    You set the plugin instance(which I don't ever use as a static) as null so that if a reload happens you don't have two references to two different instances of the plugin causing a lot of issues. On a restart the stack and heap should be cleared (because the JVM is closed, shutdown, whatever terminology you want to use).
    That is exactly what I said, not nearly as clearly or succinctly but it is what I said. #8 is something I was not condoning. Statics are used as constants that span across instances decreasing the amount of space on the heap that an object takes up. I also use statics for lists because it is universal for the object (and it doesn't need an instance of the object).
     
  10. Offline

    1Rogue

    Right, so how are you proposing you set the instance to null from within the class?

    What about the bloat from being unable to possibly unload static instances within the plugin?
     
  11. Offline

    Pizza371

    Thank you all for your huge posts, i find it really helpful.
    Currently, my object looks like this:
    Code:
    public class testObj {
    private int soemthiin, somethinelse, youknow;
    private String anothervariable, andanother;
     
    //construct
    public testObj() {
    //set variables
    }
     
    //ton of public methods for getting/setting
    }
     
    
    and example of my manager..

    Code:
    public class testObjManager {
    //currently no constructor, or anything because im unsure
    private static ArrayList<String> loadedtestobjs = new ArrayList<String>(); //init testobjs count
     
    public static ArrayList<String> getLoadedTestObjects() {
    return loadedtestobjects;
    }
     
    public static int countTestObjects() {
    return loadedtestobjects.size();
    }
     
    //etc
    }
    I'm not sure what to really understand because you both seem to be disagreeing on whether you should use statics for ease or not.

    1Rogue are you proposing I create just 1 instance of a class with a getter method or something (which might have to be static to get it?)? Or how else woould I access these without there being a different amount of, for instance loadedtestobjects?

    I understand how statics are used inside instance classes (so that instances don't affect the value).

    Also, how come I see classes like Math that use mostly static methods?

    Thanks guys! Really appreciate your help :)
     
  12. Offline

    tommycake50

    The java.lang.Math class uses Static Finals because since it's final it will never change and you never make an instance of the Math class so there is no point in per-instance variables.
     
    Pizza371 likes this.
  13. Offline

    1Rogue


    There are uses for static in classes, but bukkit plugins usually don't require them. Math is a utility class, and Loggers are a singleton class that doesn't directly affect project operation (so it's single-direction operations, a good case for singletons).

    Overall, you want to be branching your project in a way that things can access necessary managers if necessary.A relevant post on this:

    http://forums.bukkit.org/threads/getting-arraylists-from-different-classes.201697/#post-2051975

    You do not need static for this.
     
    Pizza371 likes this.
  14. Offline

    xize

    if I'm correct it only gets collected when the classloader unloads right or is that incorrect?
     
  15. Offline

    fireblast709

    If you don't set it to null, then the classloader will not even be unloaded.
     
  16. Offline

    xize

    fireblast709
    ah that makes sense, well ive to do probably alot of things but its worth since some of my plugins are pretty much static in singletons which is not needed thats probably also the reason why my server sometimes crashes when I reload because its just not thread safe anymore since I got the feeling static works more likely as a async instead of sync maybe the wrong word usage but yeah.

    well thanks for the informative posts anyway:D
     
  17. Offline

    fireblast709

    xize static has nothing to do with thread-safety :3
     
  18. Offline

    xize

  19. Offline

    Pizza371

    @everyoneinthistopic thank you (im surpsied how many people posted within the amount of time :p)
    1Rogue Thanks for explaining! I think I understand better now, will proceed to pass variables instead of static usage.
    -solved-
     
  20. Offline

    Cirno

    5 is false; I use code that stores the plugin instance as a static variable. In the onDisable(), I set the static field to null. As long as you properly manage your references to the instance, you're fine.
     
  21. Offline

    beastman3226

    Cirno
    The reference will always be there since even as a null value it can't be garbage collected (because it will always have references).

    1Rogue
    Thank you very much. I didn't think of that. Has yet to cause me problems but you opened my eyes a bit.
     
Thread Status:
Not open for further replies.

Share This Page