Results 1 to 5 of 5
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default can I construct references in this way?

    ok so I was thinking of a way to save some time... so imagine I have a number of different objects like

    employee0
    employee1
    employee2 etc

    now I want to put these objects into an array with a simple for loop, logically I want something like this

    for( int i = 0 ; i < array.length ; i++ )
    {
    array[i] = employee + i;
    }

    obviously this doesn't work because 'employee' is not a string it's a reference so I can't concatenate it like this.

    but if I want to construct my references dynamically in this way what should I do?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: can I construct references in this way?

    This is related to reflection (even though class Class is not part of the reflection package). It is somewhat advanced but it will let you do what you want. Here is an example without all the proper Exception management. Assuming six classes named as you described above.

    Java Code:
    Object [] objs = new Object[6];
    for (int j = 0; j < objs.length; j++) {
           objs[j] = Class.forName("<package>.Example" + j).newInstance();
    }
    However, there are a number of problems here. For one, if your classes don't implement a common interface you will have to check each entry in the object array to do a proper cast to access anything in the classes. This can be very cumbersome. I would suggest you re-evaluate your design.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: can I construct references in this way?

    You're close. Instead of naming them employee0, employee1, etc., you should have an array named employee and refer to the employees as employee[0], employee[1], etc.
    Get in the habit of using standard Java naming conventions!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: can I construct references in this way?

    Ah, I thought certain the OP wanted objects of different classes. I clearly missed the intent.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: can I construct references in this way?

    Quote Originally Posted by jim829 View Post
    Ah, I thought certain the OP wanted objects of different classes. I clearly missed the intent.

    Regards,
    Jim
    Yep, this is one of those questions that come from people who have done some scripting language or other and trying to apply some of that knowledge (understandably) to Java.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Construct a rectangle
    By cstokes91 in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 06:14 AM
  2. [SOLVED] Array Programming Construct?
    By iPetey in forum New To Java
    Replies: 3
    Last Post: 04-09-2009, 02:53 AM
  3. How to construct my finished program?
    By matpj in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 05:37 PM
  4. How to construct checkbox in rtf document using iText?
    By soumyanil in forum Advanced Java
    Replies: 0
    Last Post: 07-28-2008, 11:05 AM
  5. Replies: 3
    Last Post: 12-05-2007, 02:22 AM

Posting Permissions

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