Making an arraylist accessible from multiple classes

Discussion in 'Plugin Development' started by mrdude123, Jan 18, 2016.

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


    What can I do to make this arraylist accessible from several classes?
    ArrayList<Player> banlist = new ArrayList<Player>();
  2. Offline


    blok601 likes this.
  3. Offline


    @mrdude123 Possibly a getter, but according to encapsulation you shouldn't do that, adding a method to your class that allows you to add/remove elements from your list may be the best option.
    Zombie_Striker likes this.
  4. Offline


    If your Main class is called Main for example:

  5. Offline


    @blok601 @Caedus

    First @blok601 , you should almost never be in a situation where a collection should be static.

    Now, for the both of you, you should not pass around the collection's instance. Instead, the if you want to interact with those collection in any way, you should create methods that do those jobs for you (E.g. class.clear(); class.add(object), ect.) as @Konato_K said.
  6. Offline


    @Konato_K I'm not too sure how to make a method for this...
    package me.thecerealkill3r.banwave;
    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    public class BanwaveAddPlayer implements CommandExecutor{
        public boolean onCommand(CommandSender sender, Command cmd, String lbl,
                String[] args) {
            if(sender instanceof Player){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "No player was specified.");
                    return true;
                Player target = Bukkit.getPlayerExact(args[0]);   
                ArrayList<Player> banlist = new ArrayList<Player>();
            if(target != null){
                sender.sendMessage(ChatColor.GOLD + target.getDisplayName() + ChatColor.GREEN + " was added to the banwave.  The next banwave will take place in:");
                sender.sendMessage("Player could not be found.");
    } else {
        sender.sendMessage("Only players can use this command.");
            return false;
  7. Offline


    @Konato_K @Zombie_Striker There's nothing wrong with creating a getter for a Collection. Of course it depends on what you are doing with it but we don't know what the OP wants or what his code structure is like.

    @blok601 @Caedus You shouldn't use static to access things from everywhere. That is what public is for. Static is for something completely different and you don't know what it is for so don't use it and please don't suggest it anymore.

    @mrdude123 At your current code state, you can't access the ArrayList from anywhere because you aren't assigning it to a field. Create and assign your ban Collection to a field in a constructor and then we can get the ball rolling. Also please remove the Players from the Collection when they leave. Neglecting to do so causes memory leakage.
    mrdude123 likes this.
  8. Offline


    @blok601 Please, don't try to teach things wothout the kbowledge to do so.

    Learning java would possibly be th best solution here.
  9. Offline


    @teej107 Thank you.

    @mcdorli Okay, will definitely look into that, not like I already know it. Thanks.
  10. Offline


    @teej107 I agree, passing a Collection in a getter is not a real problem, however, let's assume he for some reason wants to change the List to a Set instead (because order doesn't matter, he doesn't want duplicates or whatever other reason), if another piece of code uses the getter and assigns it to a List reference then the code won't work, in my opinion is good practice to always hide the internals with other methods and this way if you ever change your list or something then everything will work perfectly, I'm not saying it's bad to return a Collection in a getter, but that using other methods to interact with it might be a better option for future changes.
    Zombie_Striker likes this.
  11. Offline


    Not that hard to get around that. Just reference the Object as a Collection. Also all Collections have an addAll method and constructors that take another Collection.
  12. Offline


    @teej107 Now let's assume he changes the Collection to a Map so he can have a Map<Player, BanDetails> instead of just keeping the player and now it can't be a collection.

    I get your point, there is nothing wrong with using a Collection (and I have used them myself several times), I just think using methods to interact with the internals of a class is has other advantages over just using the Collection.
Thread Status:
Not open for further replies.

Share This Page