Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 05:58 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
Array with objects
Hi, I have made myself a class like follows

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

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?
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 11:50 AM
Member
 
Join Date: Jul 2007
Posts: 8
mirage_87 is on a distinguished road
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=.............
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 10:46 PM
Traversing through a stack of objects, and puttin them info in an array szimme101 New To Java 1 03-25-2008 07:06 AM
Array of Objects bluefloyd8 New To Java 5 01-22-2008 08:27 PM
Help with Objects! Shorinhio New To Java 1 07-10-2007 11:32 PM
array of objects Jack New To Java 2 07-02-2007 07:24 AM


All times are GMT +3. The time now is 11:30 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org