Public Arraylists?

Discussion in 'Plugin Development' started by HyrulesLegend, Dec 1, 2013.

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

    HyrulesLegend

    How would I make an arraylist that can be accessed by all classes?
     
  2. Offline

    jorisk322

    Put it in your main class, then send an instance of your main class to your other classes through the constructor.

    EIDT:
    Doesn't have to be the main class, as long as it's accessible from the main class.
     
  3. Offline

    jessedevries20

    public static ArrayList<String> list = new ArrayList<String>();
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    jessedevries20 You don't need the static keyword. It causes all instances to share the same ArrayList.
     
  5. Offline

    xize

    you could use getters and setters instead its very easy.
    put in the same class where the ArrayList is in something like:

    Code:
    public void add(String something) {
         justAnArrayList.add(something);
    }
    
    you can also make the add method static if you like to avoid new instances if it goes over the limit.

    though setting a ArrayList to static isn't that bad if you have alot of classes using the same ArrayList ive learnt to many new instance creation could affect system performance such as gc overheat in a very extreme example.

    I also read static variables will get gc'ed when the class loader unloads i've never test it though.
     
  6. Offline

    Not2EXceL

    xize just no. The static keyword makes an object identical for every class loaded in the JVM. If you have a lot.of classes accessing that list at the same time you can run into synchronization errors. And why would you be creating too many instances? Just pass the one instance to the rest of the classes. Or use a singleton getter.for the instance.
     
  7. Offline

    xize

    Not2EXceL
    I agree about the synch errors though but in my opinion it only counts when you are creatig something huge which is why people using things like a database.
    in this case it really shouldn't mather unless static fields get persistant even when the classloader unloads due a reload which I still want to test.
     
  8. Offline

    Not2EXceL

    You can get synch errors iterating over a small list if things are being accessed from several places at once. Doesn't have to be large at all.

    You should read up more on synchronization and thread safety xize
     
  9. Offline

    fireblast709

    xize When you reload, the classloader will become unused (and gc'ed), classes will be unloaded, statics will (under normal circumstances) be gc'ed.
     
    xize likes this.
  10. Offline

    xize

    Not2EXceL
    you got a good point there I never saw it from that perspective but I think you are right.
    it kinda gives me a idea why things are async and somethings are sync now.

    I will google it though to understand it more though:p
     
Thread Status:
Not open for further replies.

Share This Page