Results 1 to 4 of 4
  1. #1
    tti123 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Accessing variables from another class

    So i basically have a GUI set up one one class with several jtextfields
    then i have listeners attached to each one and have something like this

    class gui extends jframe etcetcetcetc
    :
    String a = somethinghere.getText();
    durpdurp = Integer.parseInt(a);
    in one class



    then i have class SOMETHING that tries to use "durpdurp"

    it dosent work. . how can i change my code so class SOMETHING
    can access durpdurp in class gui?

    thanks

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

    Default

    To call methods on a class you must have a valid reference to that class, but how to get that reference will depend on how your code is structured, something we know little about. Perhaps you will need to pass a reference from one class into the other via a setter method or a constructor parameter, but I'm not sure. You might want to show us more about your code, but if you do, please use code tags so that your code is readable by us.

    Luck.

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

    Default

    Also, please give us details on what you mean by "it doesn't work". Do you get an exception? A null pointer reference? an empty String? Also, are you able to get a reference to the variable of interest, but it does not hold information that you expect it to hold? Again, more information will be helpful for us.

    And finally -- welcome to the java-forums!

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It's not at all clear what durpdurp is supposed to be, as you didn't post real code. Typically, if there is some piece of data you want to store in an object, and you want to make it available to other classes, you would store it in an instance variable or a field (same thing -- different people use different terminology) and make it available through getter and setter methods.

    Java Code:
    public class Gui extends JFrame {
            ...
            private int durpdurp;
            ...
            public int getDurpdurp() {
                    return durpdurp;
            }
            ...
            public void setDurpdurp(int durpdurp) {
                    this.durpdurp = durpdurp;
            }
    }
    You could also expose the field directly, like this:
    Java Code:
            public int durpdurp;
    ...and then you would not need the getter and setter methods, but this is generally not a good idea, as it breaks the concept of encapsulation that is at the heart of object-oriented programming.

    -Gary-

Similar Threads

  1. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 02:21 AM
  2. Accessing Variables From a Different Class?
    By Sterling in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 06:01 PM
  3. Replies: 4
    Last Post: 10-22-2009, 06:45 PM
  4. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 05:36 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 PM

Posting Permissions

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