Results 1 to 9 of 9
Thread: Accessing array in other class
- 12-26-2008, 12:19 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
-
You can create a public getter method in the class that holds the array and then have the other class call that public method on an object of the first class (again, the one that holds the array).
By the way, can you clarify this statement here?:
DB1.database[0] DB2.database[0] DB3.database[0] (etc..) based on an integer i.
- 12-26-2008, 12:27 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 8
- Rep Power
- 0
You could store the DBs in an array, as such:
Java Code:DB[] dbArray = new DB[3]; dbArray[0] = new DB(); dbArray[1] = new DB(); dbArray[2] = new DB(); System.out.println(dbArray[2].database[0]);
Did that make much sense to you?
-
Ah, I think I see now. Behold's explanation seems to be spot on. Please ignore what I posted previously.
- 12-26-2008, 01:42 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Java Code:DB[] dbArray = new DB[3]; dbArray[0] = new DB(); dbArray[1] = new DB(); dbArray[2] = new DB(); System.out.println(dbArray[2].database[0]);
DB1.java
DB2.java
DB3.java
etc...
DB1.java contains this:
Java Code:class DB1 extends Test { public static String[][] database = { {"a", "text sfkejfle kjsekfj sekfj sk"}, {"b", "fjkjefk esfkje fksejf sjefksekj"} }; }
So what I would like to do in fact (in my main Test.java file) is this:
Java Code:for (int i=1; i<109; i++) { helloForm.append({"DB"+i}.database[0][0]); }
Sorry, it really is one of my first days Java. I'm used to PHP (that's why I'm writing {"DB"+i} which would be similar..)Last edited by ce3c; 12-26-2008 at 01:45 AM.
- I've got 108 class files... putting "dbArray[*] = new DB();" each time doesn't seem the good way to me.
Java is very different from PHP and you can't just jump from one type of programming to the other. For instance variable names are not so flexible in Java so what you are trying to do to manipulate them is doomed to fail.
Strong word of advice: stop doing your project now and go through a text book and / or tutorial on Java. If not, you are going to be writing a monstrously large program that you will just have to discard later as it will be impossible to maintain or debug.Last edited by Fubarable; 12-26-2008 at 02:06 AM.
- 12-26-2008, 02:05 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 8
- Rep Power
- 0
Wow. That seems impractical. Wouldn't it be possible to have one single class, called "DB", which takes a String array like this:
Java Code:class DB extends Test { public String[][] database; //note the removal of static public DB(String[][] sArr) { database = sArr; } }
Java Code:String[][] db = { {"a", "text sfkejfle kjsekfj sekfj sk"}, {"b", "fjkjefk esfkje fksejf sjefksekj"} }; DB object = new DB(db); String[] tmp = object.database[0];
- 12-26-2008, 02:03 PM #8
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Java Code:Perhaps creating an array of DB objects based on what's in the text file.
Isn't there a function like StringToClassName() ?
I'll try to put everything into a different entry of an array, but I'm afraid the 65535 charlimit-error will rise again.
- 02-23-2009, 12:07 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 4
- Rep Power
- 0
Bumping up this old thread..
been away a while but found a solution to my problem now.
For other noobies who have the same problem:
I didn't know the class name I wanted to load before the app was executed.
The solution was (quite) simple though:
If you would like to "DB1.class" or "DB2.class" depending on an int,
this is the way:
Java Code:try { Class.forName("DB1").newInstance(); // or Class.forName("DB"+int).newInstance(); where int=1; // equals "new DB1();" } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }
Similar Threads
-
Accessing list out another class
By Preethi in forum New To JavaReplies: 23Last Post: 10-26-2008, 03:54 PM -
Accessing inner class from outer class (an example)
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 10:03 AM -
An example of accessing outer class from inner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 10:01 AM -
Inner class accessing outer class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:59 AM -
Accessing one class from another class through swing
By kbyrne in forum AWT / SwingReplies: 5Last Post: 01-03-2008, 08:54 AM
Bookmarks