Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Initializing an Array of Objects

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Initializing an Array of Objects

    NOTE: This is more of a post asking for a little clarification, rather than one asking for help.

    Java does not seem to actually create any objects when you define an array specifically of objects, unlike if it was an array of primitives which are initialized to their type's default. The following code demonstrates what I'm getting at:

    public class GridSquare
    {
        private int x, y, width, height;
     
        public GridSquare() //Constructor
        {
            this.x = 0;
            this.y = 0;
            this.width = 0;
            this.height = 0;
        }
        public void setGridLocation(int newX, int newY, int newWidth, int newHeight)
        {
            this.x = newX;
            this.y = newY;
            this.width = newWidth;
            this.height = newHeight;
        }
    }

    public class Map
    {
        GridSquare[][] gridSquares = new GridSquare[10][10]; //This only creates a 10 by 10 array of Null GridSquare objects
     
        public Map() //Constructor
        {
            gridSquares[0][0].setGridLocation(50,50,50,50); //A test location that shows all gridSquares locations are Null
        }
    }

    Now, I've done a fair share of Google searching to reach this conclusion, and the answer I seem to get is that I should (separately) initialize every single array cell with the new command, such as described by these sources:
    Array of objects initialization Problem - Java - Forums at ProgrammersHeaven.com
    Explain how to initialize an array of objects

    This seems like a roundabout way to accomplish what I would think Java could handle on its own. Is there any particular reason that Java is unable to actually create an object for each cell and reference it? With a proper Constructor written for the object in the array so that the proper data gets loaded, I'd imagine you could skip all the extra work.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Initializing an Array of Objects

    Java does not seem to actually create any objects when you define an array
    That is true. How would java know how to call the class's constructor to create the objects? It leaves the slots in the array with null values. Even if it would use a default constructor, that could be a lot of useless work if the user wants meaningful data in the array and has to assign values with data to all the slots.
    I should (separately) initialize every single array cell with the new command,
    Yes that is how you have to it.
    Last edited by Norm; April 20th, 2012 at 06:27 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Loop through a 2d array of objects
    By ssjg0ten5 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 28th, 2012, 09:53 PM
  2. Help with storing objects in array? :(
    By spoonemore11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 2nd, 2011, 04:28 PM
  3. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM
  4. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM