Question on objects in array.

Discussion in 'Application Development' started by LeonR, Feb 26, 2010.

  1. LeonR

    LeonR Member
    15

    Feb 22, 2010
    18
    0
    15
    Hi,

    I'm new at java, so please excuse my terminology!

    I also cant remember the exact code off the top of my head, so you will have to guess what I mean if its wrong :D

    Basically I want to create multiple objects of the same class, but I also want to keep a reference/track to each one. What would suit me, and what seems to work is if I create an (object array?) array.. such as...

    MyClass myObj[] = new MyClass[10]; << is that right?

    And then I can create them like

    myObj[0] = new MyClass();

    myObj[0].myMethod();

    Again I apologise for the code, its a steep learning curve for me as i'm more used to .net, and even then I dont implement classes and objects like java.


    Is it an acceptable way to manage/use multiple objects in this manner? It's not considered bad practice?


    For example, I might want to create a few objects, but keep referencing each one from another thread, so I would need to be able to call each one individually and it seems from what im trying to do, an array of objects would work best?


    Cheers! :cool:
     

    Advertisement
  2. alostpacket

    alostpacket Over Macho Grande?
    VIP Member
    513

    Nov 29, 2009
    7,978
    3,603
    513
    Android App Developer
    NY
    Looks ok to me, you can also use a typed array list

    ArrayList<MyClass> myList = new ArrayList<MyClass>();

    myList.add( new MyClass() );

    myList.get( 0 ).myMethodFromMyClass();
     

Share This Page

Loading...