Results 1 to 5 of 5
- 07-06-2012, 02:59 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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
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?Java Code:public final int height = 480; public final int width = 360;
-
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!
- 07-06-2012, 02:46 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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.
- 07-06-2012, 03:26 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 07-06-2012, 10:00 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
public class Hello() {
By Alien in forum IntroductionsReplies: 2Last Post: 03-07-2012, 08:11 AM -
Call a public string variable
By onstock in forum New To JavaReplies: 3Last Post: 11-23-2011, 12:25 PM -
Making an array public or any variable to use across classes and Java files?
By Jonatan10 in forum New To JavaReplies: 12Last Post: 12-12-2010, 06:04 PM -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM -
Public class variable
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks