Static Context

Alright, this problem is a little difficult to explain. It's for an RPG game. Spell is a working class (builds me a new variable)

publicclass Setup

{

String dats[] =new String[10];

publicvoid loadDat()

{

/*code for opening data file is unimportant, assume it works and is a string in dats[0]*/

}

public Spell getSpell(String a)//This returns a spell based on ID tag

{

//this searches through dats[0] for "a" and builds a Spell that it declares as foundspell; assume it works

return foundspell;

}

//This returns an ArrayList of spells (of one type) that the user can cast

public ArrayList searchSpell(int type,int req)

{

ArrayList spells =new ArrayList();

/*this uses "getSpell()" to search the dats[0] for certain requires; assume it works as intended as puts all the spells into spells*/

return spells;

}

}

loadDat() is executed as the start of the program (in another class).

So, I need it to be non-static because I need the dats[0] to be accessible always (without running it everytime searchSpell() is called). But when I call searchSpell() from another class (that's not always static, its non-static too) it says: "non-static method cannot be referenced by a static context". If i declare everything as static (the methods above). It says hte same error, but points at the variable dats[0].

Any thoughts?

Sorry it's so confusing, if anyone has any idea, please tell, I can probably add more details if you think you need to know something I didn't write in.

[2395 byte] By [dontocsataa] at [2007-10-2 22:14:21]
# 1
I fixed the problem. All I had to do was declare dats[] as static.static String dats[] = new String[10];
dontocsataa at 2007-7-14 1:31:17 > top of Java-index,Java Essentials,Java Programming...
# 2
Ahhh the old "I made it static and now it works fix....."Booooooooooooooo
Norweeda at 2007-7-14 1:31:17 > top of Java-index,Java Essentials,Java Programming...
# 3

ktm5124, you're making an instance of the class, yes I use that, but (for some reason) when I applied that same idea to my above problem is caused an excessive wait time (the program would sit for 20-30 seconds) and then continue. There is no wait-time with the fix I used.

Please, if you have any suggestions, I would love to make this better/more efficient.

dontocsataa at 2007-7-14 1:31:17 > top of Java-index,Java Essentials,Java Programming...
# 4
One object shouldn't cause a 20-30 second wait time =pTry compiling my code.Your "fix" is bad. Don't use static. It's clinically proven to be bad for your health.
ktm5124a at 2007-7-14 1:31:17 > top of Java-index,Java Essentials,Java Programming...
# 5

> I fixed the problem. All I had to do was declare

> dats[] as static.

>

> static String dats[] = new String[10];

Umm,

Yeah, it is an example of a bad designing an there are several reason for this.

One of them is that we generally use static keywords for member of classes, not for referece variables to any classes.

samue-1a at 2007-7-14 1:31:17 > top of Java-index,Java Essentials,Java Programming...
# 6

I encounterd a problem which looks like your own problem and I also solved it by using static keywords in spite of knowing a bad design sample.

If the program is a project for you and you instructor examines it, please try to use more efficient way to solve it.

For example, try searching about Singleton pattern.

samue-1a at 2007-7-14 1:31:17 > top of Java-index,Java Essentials,Java Programming...