putting a non static variable in a static variable
I am trying to move the content of xp to skillXp, and I am getting this error "Cannot make a static reference to the non-static field xp"
How can I put the contents of non static var xp, into static var skillXp?
Any help or advice would be great
Here's what I have
Code :
private final double xp[] = new double[SKILL_NUMBER];
public static double skillXp[] = new double[SKILL_NUMBER];
public static void setNewXP (int skillid) {
skillXp[skillid] = xp[skillid];
}
Re: putting a non static variable in a static variable
Why do you want to do this? This seems a strange way to organize your code.
Note that in the static world xp has no meaning as it simply doesn't exist. It requires an instance of the class to exist. So to fully answer your question you need to explain a lot more about what is going on and what you're trying to do.
Re: putting a non static variable in a static variable
I'd like to be able to access xp in another class
Re: putting a non static variable in a static variable
Quote:
Originally Posted by
beennn
I'd like to be able to access xp in another class
That's not how you do this, by using statics, and in fact you should strive to avoid use of statics as much as possible except in certain situations where it is useful (and this is not one).
To have another class be able to interact with this field, that other class needs an instance of an object of this class, and it can then get the information by calling a method on the instance, or it can alter the object's behavior by calling a method. Again static is most definitely not the way to go.
Re: putting a non static variable in a static variable
I attempted to make an instance of the Skill class which contains the variables, however I was getting "The constructor Skill() is undefined"
Re: putting a non static variable in a static variable
Quote:
Originally Posted by
beennn
I attempted to make an instance of the Skill class which contains the variables, however I was getting "The constructor Skill() is undefined"
Then you'll need to look at the Skill class code and only use constructors that are available for use.