Results 1 to 2 of 2
Thread: Array with objects
- 07-24-2007, 03:58 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Array with objects
Hi, I have made myself a class like follows
And would like many instances of this class. I triedJava Code:class State { static int[][] grid = new int[2][2]; static int parent; static int depth; }
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?Java Code:State[] states = new State[1000];
Thanks.
- 07-25-2007, 09:50 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 8
- Rep Power
- 0
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]
obj[0].grid=.............
Similar Threads
-
Creating an array of nonprimitive objects
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:46 PM -
Traversing through a stack of objects, and puttin them info in an array
By szimme101 in forum New To JavaReplies: 1Last Post: 03-25-2008, 05:06 AM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
Help with Objects!
By Shorinhio in forum New To JavaReplies: 1Last Post: 07-10-2007, 09:32 PM -
array of objects
By Jack in forum New To JavaReplies: 2Last Post: 07-02-2007, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks