Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Rep Power

    Default Array with objects

    Hi, I have made myself a class like follows

    Java Code:
    class State  {
    	static int[][] grid = new int[2][2];
    	static int parent;
    	static int depth;
    And would like many instances of this class. I tried

    Java Code:
    State[] states = new State[1000];
    But it does not set each instance correctly when I try states[0].grid = .... It treats it like one instance. What is the correct way to do this?

  2. #2
    mirage_87 is offline Member
    Join Date
    Jul 2007
    Rep Power


    the reason is that u r using "static" access for grid.
    wen a variable is defined static it means that it is defined not for one instance of the class instead it is unversal for all the instances the class has.

    so if i say
    static int i=0; then all the instances of class will have i=0;
    and ny changes to i will affect the value of i in all instances.

    so the solution is to use public or private access for these variables instead of static

    so the class shld look like......
    class State {
    public int[][] grid = new int[2][2];
    public int parent;
    public int depth;

    state[] obj=new state[1000]

Similar Threads

  1. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  2. Replies: 1
    Last Post: 03-25-2008, 05:06 AM
  3. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  4. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:24 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