Results 1 to 2 of 2
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Getting objects of a class

    I have a class named Facility. I created 3 objects of it. During program execution, more objects will be created. Now I want some way to get all the objects of the class named Facility. Some iterator or something would do.

    Java Code:
    Facility obj1 = new Facility ();
    Facility obj2 = new Facility ("Facility NY",10);
    Facility obj3 = new Facility ();

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default

    I think you should store your object is some sort of array or collections and get it later by iterating the array of collections. You can then check whether your class is an instance of Facility class using the instanceof construct.

    Java Code:
    for (int i = 0; i < collection.size(); i++) {
        Object obj = collection.get(i);
        if (obj instanceof Facility) {
            // do something with the object
        }
    }
    Last edited by wsaryada; 12-04-2007 at 12:26 PM.

Similar Threads

  1. Color objects
    By CyberFrog in forum New To Java
    Replies: 4
    Last Post: 04-01-2008, 12:41 AM
  2. Getting objects from a list
    By markyoung1984 in forum New To Java
    Replies: 4
    Last Post: 03-13-2008, 10:45 PM
  3. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM
  4. how can objects themselves be copied???
    By ishakteyran in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 10:04 AM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •