Solved Class variables resetting

Discussion in 'Plugin Development' started by pookeythekid, Dec 17, 2014.

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

    pookeythekid

    Although I've looked over my code about twenty times, I can't seem to find any reason why my HashMap and JavaPlugin variable seem to be erasing themselves the moment they're used in a method. I've tried several different solutions, but I've only come up with one working one: (yes, I know it's frowned upon, but) making them static. I figured I wanted someone else's opinion (and working solution) before I proceeded with these statics in my class.

    The code links below are the best-working versions of my plugin, although there's still a tiny bit of debugging to do.

    Link to the code (ignore the random debug printouts): http://pastebin.com/WizB19Mm

    Main class (which contains nothing but an onEnable): http://pastebin.com/w05Zz2Ah

    Also, I have seen this StackOverflow post, and found that the only apparent solution is by using statics.
     
  2. Offline

    Webbeh

    This is actually a really good use of the static. (for the hashmap, not for the JavaPlugin)
     
  3. Offline

    pookeythekid

    @Webbeh Yeah, I figured there was nothing wrong with the HashMap. But unfortunately the JavaPlugin has the same problem.
     
  4. Online

    timtower Administrator Administrator Moderator

    @pookeythekid In your first constructor you have this: new MobCannon(plugin, map, true, true);
    Won't do anything besides creating a new object and then destroy it right away.
     
  5. Offline

    pookeythekid

    @timtower I figured that was the only way to get the code in the second constructor to execute with the EntityTypes I wanted it to have. Is there no shortcutting this; will I just have to use the second constructor properly when the plugin enables?

    Edit: I wanted to do something of a shortcut because this is going to be contributed to the MVPGadgets community project, and I wanted it to be easy to use -- that is, if it works.
     
  6. Online

    timtower Administrator Administrator Moderator

    @pookeythekid Make a function out of it instead of a constructor.
     
  7. Offline

    mythbusterma

    @pookeythekid

    It seems to me rather than maintaining an instance of the class, whenever you're using it you're instantiating it again instead of passing around an instance. Pass the instance and remove the lazy use of static, even in that exact same post you linked to they provide the correct solution.

    [​IMG]
     
  8. Offline

    teej107

    @pookeythekid Constructors should all call (or lead to calling) one constructor or the same method to insure that nothing gets missed when creating an object.

    Code:
    public class Example
    {
        private String s;
    
        public Example(String s)
        {
            this.s = s;
        }
    
        public Example(Object object)
        {
            this(object.toString());
        }
    }
    This example is very simple but I hope it gets the point through.
     
    mythbusterma likes this.
  9. Offline

    pookeythekid

    While you two were typing, I just followed @timtower 's advice. Thanks Tim.

    Edit: @mythbusterma I believe I tried that solution doing it correctly.
     
Thread Status:
Not open for further replies.

Share This Page