Hashmaps in different classes

Discussion in 'Plugin Development' started by RoBaMe, Jul 23, 2015.

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

    RoBaMe

    Hi! I know it's a stupid question but how do use hashmaps in different classes?
     
  2. Offline

    finalblade1234

    Make the hashmap static (static HashMap<String, Integer> <-- like that) then use classname.hashmapname to access it :)
     
  3. Offline

    RoBaMe

    @finalblade1234 ok so i did this : public static HashMap<UUID, Boolean> live = new HashMap<UUID,Boolean>(); and then did in the other class SpecMain.live; but it did work :/
     
  4. Offline

    finalblade1234

    @RoBaMe
    Remember to put a method after .live,
    example: SpecMain.live.put(player.getUUID(), true);
     
  5. Offline

    teej107

    No. You shouldn't use static just because it makes Objects easily accessible. It is not how it should be used.

    @RoBaMe use "Getters" to obtain Objects from different classes. As long as you have a instance of the class available, you can get anything from inside the class using getters.
    If you are having trouble understanding what I mean, just google it. This is the kind of thing you should have already learned before trying to write a Bukkit plugin.
     
  6. Offline

    567legodude

    @teej107 I don't use static objects, but what is it that makes them bad?
     
  7. Offline

    teej107

  8. Offline

    567legodude

    @teej107 So then would it be okay to use a static map to store info like the currently selected color of a player (Map<UUID, Color>) That can be accessed by all the classes that need to grab their color. Or do I really have to save an instance of the whole class and access it through methods?
     
  9. Offline

    teej107

    @567legodude
    It's not hard to pass info around through parameters.
     
  10. Offline

    Hawktasard

  11. Offline

    567legodude

    @teej107 @Hawktasard I mean in a class made to store only one thing. I only need one instance of the map, which is why I was wondering if making it static was okay in that situation.

    Usually when I set up a class system, I have a main class to store all the instances, and each class uses the main class to reference the other classes.

    I can see why you don't want to use static if you need multiple instances of a class, but if I just need a single instance of the map, then would that be an appropriate place to use static?
     
  12. Offline

    teej107

  13. Offline

    1Rogue

    Here's a better thought: what's preventing you from not using static?
     
  14. Offline

    567legodude

    @1Rogue Nothing, I did that as a test, and it worked perfectly fine. Easily able to keep a map of data.
     
  15. Offline

    1Rogue

    Well then there's your answer, you don't need static.
     
Thread Status:
Not open for further replies.

Share This Page