Results 1 to 6 of 6
  1. #1
    bugmenot is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default Custom class in an array?

    I was wondering if it was possible to put a custom class in an array. So that way you could do like:
    Java Code:
    //something.java
    class something {
    boolean visible;
    int x,y;
    }
    
    //blah.java
    //...
    something[4].x = 40;
    something[4].y = 30;
    something[4].visible = true;
    //...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You bet it's possible, and it's done often. Why not give it a try and see what happens?

  3. #3
    bugmenot is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    Well this is what I tried:
    Quote Originally Posted by brick.java
    /*
    * Properties for each brick
    */

    class brick {
    boolean visible;
    int x,y,width,height;
    }
    Quote Originally Posted by BreakOut.java
    /**
    * BreakOut
    */

    import java.awt.*;
    import java.applet.*;

    public class BreakOut extends Applet {

    brick block[] = new brick[20];

    public void init() {
    block[3].visible = true;
    block[4].x = 60;
    System.out.println(block[4].x);
    }

    public void paint(Graphics g) {

    }
    }
    And it's returning:
    --------------------Configuration: BreakOut - JDK version 1.6.0_13 <Default> - <Default>--------------------
    java.lang.NullPointerException
    at BreakOut.init(BreakOut.java:14)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:619)

    Process completed.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah, you are now learning the difference between an array of primitive, say an int array, and an array of objects, say a brick array. With the primitive array, all you have to do is declare the array and use it -- it's ready immediately for your use.

    With an object array on the other hand, you have to declare the array first, and then you have to initialize the objects in the array before you can use them. Think of creating an object array as if you were creating a parking lot. You have to fill it with cars first before you can use a car. So for this code:

    try changing your init to:
    Java Code:
      public void init()
      {
        // initialize every brick in the block array
        for (int i = 0; i < block.length; i++)
        {
          block[i] = new brick();
        }
        block[3].visible = true;
        block[4].x = 60;
        System.out.println(block[4].x);
      }
    and then see if it works

  5. #5
    bugmenot is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    Perfect, thanks a lot.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  2. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 03:12 PM
  3. Custom Class, Wrapper/Subclass?
    By Moncleared in forum Advanced Java
    Replies: 3
    Last Post: 02-10-2009, 12:08 AM
  4. Demonstration of the custom InputDialog class
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:56 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 12:23 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
  •