ArrayLists access-able via another class

Discussion in 'Plugin Development' started by Nachoman44, Feb 26, 2014.

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

    Nachoman44

    I've looked around for info on how I could accomplish getting an arrayList and using it in another class other than my main one (my main class contains the array list) and all i've seen are constructors. Every constructor tutorial shows me how to set one up, but never tells me how to actually use something from that class...
    Here's my code:
    I'm trying to make the arrayList named teamRed and the arrayList named teamBlue access-able in my Listeners class, here it is:

    Paintball - main class
    Code:
    public class Paintball extends JavaPlugin{
        private  ArrayList<Player> teamRed = new ArrayList<Player>();
        private ArrayList<Player> teamBlue = new ArrayList<Player>();
     
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(new PaintballListeners(), this);
        }
        public boolean onCommand(stuff goes here){
            do command stuff
    }
    
    and here is my listeners class where i want to be able to use the arrayLists
    Code:
    public class PaintballListeners implements Listener{
        //I want to be able to get hold of the arrayList teamRed and teamBlue. but how?
    }
     
  2. Offline

    MrInspector

    Do Paintball.<arraylist>
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    Nachoman44
    Sounds like you should read http://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
    Be aware that something has to be visible in order to be used.

    Worse suggestion. It is not necessary to make it static. In fact it could be potentially harmful.

    EDIT: I think think of worse things.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  4. Offline

    Nachoman44

    I've taken your advice and done this:
    Paintball class (main)
    Code:
    public class Paintball extends JavaPlugin{
        static ArrayList<Player> teamRed = new ArrayList<Player>();
        static ArrayList<Player> teamBlue = new ArrayList<Player>();
     
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(new PaintballListeners(), this);
        }
    PaintballListener class
    Code:
    public class PaintballListeners implements Listener{
        ArrayList<Player> teamRed = Paintball.teamRed;
        ArrayList<Player> teamBlue = Paintball.teamBlue;
    }
    Just to confirm, is this correct? will the "teamBlue" and "teamRed" still hold the same list as the ones in Paintball class? I appreciate your help! :)
     
  5. Offline

    Dread9Nought


    Definitely not the worst suggestion in the world.

    You need for starters to make the objects public and then you need to decide if its appropriate to have them static, probably not though, in which case you need to a way to access the class instance and then touch on the ArrayLists.
     
  6. Offline

    Alshain01

    While I don't sit in the "never use static variables" camp, in this case it can be avoided. Does he even need his ArrayLists to be in his JavaPlugin class? If the Listener will be the primary user of the array lists, put them in that class. Even if you need a command to add players, make an setter/getter for that and don't register it as an anonymous listener.
     
    NathanWolf likes this.
  7. Offline

    Dread9Nought


    Not going to argue with you there, buddy!

    Of course it can be avoided! Merely pointing it that it wasn't the worst suggestion in the world :)
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    The variable actually doesn't even need to be public, it merely has to be visible. The important idea here is that the array lists should be an instance variable.

    The other proper thing to do is to use accessor methods, which do not need to be public either. The idea here is that the methods make your code more stable and allows you to better check for prerequisite conditions.
     
  9. Offline

    TryHardCoder

    Change 'Private' to 'public static'

    public static = accesable by all classes
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    There is no advantage or need for the array variables to be static. In fact, most of this thread has been promoting the proper encapsulation of data.
     
  11. Offline

    MrInspector

    I've actually never done it like that so I wouldn't know o-o

    I just use FOR EXAMPLE
    Code:java
    1. if(Paintball.teamBlue.contains(player.getName){ // what ever bla blah bla
     
  12. Offline

    Dread9Nought


    Aye!

    There are cases where static would be appropriate but looking at the code context of the OP, static currently has no use here.
     
  13. Offline

    PatoTheBest

  14. Offline

    xMrPoi

    Make the ArrayLists in the main class public and then you can delete where you make new arraylists at the beginning of the class and use Paintball.redTeam, blueTeam, etc. Also, if you did get the ArrayLists at the top of the class, I don't think it would work. I think it's only getting the ArrayList when it first loads so those would be empty if you tried using them in that class. I could be completely wrong about this but yeah.
     
Thread Status:
Not open for further replies.

Share This Page