Results 1 to 5 of 5
  1. #1
    gogonimago is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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
    Java 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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!

  3. #3
    gogonimago is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    gogonimago is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Class can't see public variable in the same package

    ...well this is awkward

Similar Threads

  1. public class Hello() {
    By Alien in forum Introductions
    Replies: 2
    Last Post: 03-07-2012, 08:11 AM
  2. Call a public string variable
    By onstock in forum New To Java
    Replies: 3
    Last Post: 11-23-2011, 12:25 PM
  3. Replies: 12
    Last Post: 12-12-2010, 06:04 PM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •