Getting a HashMap and using it in another class

Discussion in 'Plugin Development' started by Vamure, Feb 8, 2015.

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

    Vamure

    Hey.

    DeathBan class:
    Code:
    public HashMap<String, Long> deathbanned;
    
        public DeathBan() {
            this.deathbanned = new HashMap<String, Long>();
        }
    // code
    // code

    Border class:

    Code:
     // player event
           DeathBan db = new DeathBan();
                 db.deathbanned.put(playerName, System.currentTimeMillis());
    
    This doesn't work. The player's name (string) doesn't get added to the HashMap in the Border class.
     
  2. Offline

    Gater12

    @Vamure
    Of course it doesn't work. You just created a new DeathBan instance and added the name to it inside which in turn does really nothing.
     
  3. Offline

    _Filip

  4. Just make the HashMap static
     
    1Camer0471 likes this.
  5. Offline

    SuperOriginal

    @BugsyFTW If I wasn't too lazy to find a facepalm picture, there'd be a huge one right here.

    *Facepalm*
     
  6. Offline

    1Rogue

    http://bukkit.org/threads/how-to-us...-that-extends-javaplugin.285074/#post-2613361

    http://bukkit.org/threads/using-a-string-in-different-classes.184140/#post-1916182
     
  7. Offline

    teej107

  8. Offline

    1Camer0471

    Whats wrong with making static HashMaps?
     
  9. Offline

    1Rogue

    Static isn't a tool to make accessing fields easier. What if you had this:

    Code:java
    1. public class MyClass {
    2. public static Map<?, ?> myMap = new HashMap<>();
    3. }


    How would you handle me doing this?:

    Code:java
    1. MyClass.myMap = null;


    You could of course add a final modifier to the map, but it's the same principle:

    Code:java
    1. MyClass.myMap.clear();


    If you blatantly expose your fields in a class then you open yourself to hours of frustration later in trying to track down things that call your map badly, having to implement solutions like:

    Code:java
    1. public static final Map<String, String> myMap = new HashMap<String, String>() {
    2.  
    3. @Override
    4. public void clear() {}
    5.  
    6. @Override
    7. public String remove(String remove) {
    8. StackTraceElements[] elm = Thread.getCurrentThread().getStackTrace();
    9. if (elem.length > 2) {
    10. //conditional code on calling depending on class members
    11. }
    12. }
    13.  
    14. }


    Whereas you can simply just use mutator and getter statements to allow only the functionality you desire:

    Code:java
    1. private final Map<String, String> myMap = new HashMap<>();
    2.  
    3. public Map<String, String> getMap() {
    4. return Collections.unmodifiableMap(this.myMap);
    5. }
    6.  
    7. public String getValue(String key) {
    8. return this.myMap.get(key);
    9. }


    The whole point of this is that you should try to make a class as unmodifiable as possible while still allowing the operations you want.
     
  10. Offline

    teej107

  11. Offline

    Vamure

  12. Offline

    1Rogue

    Instead of creating a new DeathBan instance, you should pass your other one via constructor.
     
  13. Offline

    coasterman10

    [​IMG]
     
    johnny_boy and Skionz like this.
Thread Status:
Not open for further replies.

Share This Page