Class can't see public variable in the same package
So I'm using Slick 2d to mess around and the way it works is it's a state based engine which means that in a game, there's different states, menu, play etc. Slick works with those states to create a functioning game. Now my question requires no knowledge of slick, it's a vanilla Java problem. I want to set my screen height and width to two separate public variables because my Menu class needs to access them (they're declared in the main Game class). The problem is that when I try to declare the two variables like Code:
public final int height = 480;
public final int width = 360;
in the Game class and then try to access them in the Menu class in any way (set them to other variables or pass them in to a method) I get an error that says that "height cannot be resolved to a variable" This leads me to think that the JVM doesn't know about that variable, but why not! It's public! Can anyone help me with my woes?
Re: Class can't see public variable in the same package
They are public *instance* variables, and thus only exist in instances of whatever class they are declared in. So to access them, you'd first have to create an object of that class. But I'm thinking that your error is really that you forgot to make them static. If they're static, then the variables become class variables and don't need an object of the class to get at them.
Strongly consider going through an intro to Java textbook or tutorial to read more on this subject and to get a firm grip on Java foundations as it will make your coding with Slick a *lot* easier, trust me!
Re: Class can't see public variable in the same package
The problem is that the method that I want to access these variables is non static, therefore it can't access static variables.
Re: Class can't see public variable in the same package
It's the other way around.
non-static can access static.
Static cannot access non-static.
Re: Class can't see public variable in the same package