Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: Initializing and array of object via their constructors

  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Initializing and array of object via their constructors

    How would I call the constructor in an array of objects. Example:

    Java Code:
    Object objects [] = new Object [10];
    
    for (int i = 0; i<object.length; i++){
    objects[i](constructor parameter?)
    }
    I hope that makes sense. I want to pass a set of parameters to each object's constructor in the array. Doing it the way shown in the example obviously does not work, so how would I approach this?

    Thanks in advance!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    Object obj = null;
    How would you make this non null? It's very similar to do this to an array of objects, replace obj with objects[i].

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    To assign values to array elements:
    <arrayname>[ix] = value; // assumes compatible data types

    for a new instance of the Object class, use the new statement to create the value to be stored.
    object[i] = new Object();
    JeffGrigg likes this.

Similar Threads

  1. Replies: 1
    Last Post: 05-25-2011, 09:01 AM
  2. Array constructors
    By StanO in forum New To Java
    Replies: 4
    Last Post: 11-05-2010, 09:01 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 PM
  5. Initializing property object from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:55 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
  •