• Lucid Dreaming - Dream Views




    Results 1 to 8 of 8
    1. #1
      I am become fish pear Abra's Avatar
      Join Date
      Mar 2007
      Location
      Doncha Know, Murka
      Posts
      3,816
      Likes
      542
      DJ Entries
      17

      quick java question

      So I'm making a program for the lulz, and I've realized something. Almost all of my classes need to access the same coordinates, x and y, and half of them need to alter x and y. So, I have the coordinates as public ints. But it's going to be a pain to have to type locationList[4][5].x every time I want to alter or use x.

      Isn't there a way I could use x and y as if they were a natural part of every class? How would I do that?
      Abraxas

      Quote Originally Posted by OldSparta
      I murdered someone, there was bloody everywhere. On the walls, on my hands. The air smelled metallic, like iron. My mouth... tasted metallic, like iron. The floor was metallic, probably iron

    2. #2
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes. Create a java file called Global, or something like that, then include it in every class that needs it. From there you can access and manipulate them directly.

      Code:
      //global
      int x, y;
      Code:
      //Some class
      #import "global"; //I don't remember the exact syntax for this.
      
      int somevalue = global::x;
      Been a while since I've used java, so the syntax might be a little off, but that's how to do it.

    3. #3
      DuB
      DuB is offline
      Distinct among snowflakes DuB's Avatar
      Join Date
      Sep 2005
      Gender
      Posts
      2,399
      Likes
      362
      Quote Originally Posted by Abra View Post
      Isn't there a way I could use x and y as if they were a natural part of every class? How would I do that?
      It's been a while since I've used Java as well, but if I understand your question correctly, you can simply declare x and y as global variables. This is accomplished by declaring them outside of all of your methods.

      E.g.,

      Code:
      public class programForLulz{
      
         int x, y;
      
         public static void main etc...

    4. #4
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by DuB View Post
      It's been a while since I've used Java as well, but if I understand your question correctly, you can simply declare x and y as global variables. This is accomplished by declaring them outside of all of your methods.

      E.g.,

      Code:
      public class programForLulz{
      
         int x, y;
      
         public static void main etc...
      Those would be global to the class, not between classes

    5. #5
      DuB
      DuB is offline
      Distinct among snowflakes DuB's Avatar
      Join Date
      Sep 2005
      Gender
      Posts
      2,399
      Likes
      362
      Ah yeah.

    6. #6
      Member TimeStopper's Avatar
      Join Date
      Oct 2007
      Gender
      Location
      In select theaters.
      Posts
      209
      Likes
      1
      You could have a superclass with those variables that all your classes extend.

      Or you could make them static variables, if you only need one set of those variables.
      "Reality is merely an illusion, albeit a very persistent one." -Einstein

    7. #7
      I am become fish pear Abra's Avatar
      Join Date
      Mar 2007
      Location
      Doncha Know, Murka
      Posts
      3,816
      Likes
      542
      DJ Entries
      17
      Quote Originally Posted by TimeStopper View Post
      You could have a superclass with those variables that all your classes extend.

      Or you could make them static variables, if you only need one set of those variables.
      Yeah, superclass... I think that's what I need. (Syntax?)

      This is where my intro class never finished. We started interfaces, but then the professor was all "Wait. That's not going to be on the exam. Let's talk more about exceptions instead."
      Abraxas

      Quote Originally Posted by OldSparta
      I murdered someone, there was bloody everywhere. On the walls, on my hands. The air smelled metallic, like iron. My mouth... tasted metallic, like iron. The floor was metallic, probably iron

    8. #8
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by TimeStopper View Post
      You could have a superclass with those variables that all your classes extend.

      Or you could make them static variables, if you only need one set of those variables.
      I don't think you understand the concept of inheritance You don't need to inherit anything, just needs to be included.

    Bookmarks

    Posting Permissions

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