Thread: Public Variable

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    73
    Rep Power
    10

    Public Variable


    This is my first time working with Java or GUI elements and I have run into a problem. I am looking to write to a variable every time a button is pressed.

    I started with
    Code:
    class getPage {
        public int page;
    }
    then in an onClick method, I use the following:
    Code:
    getPage getpage = new getPage();
    getpage.page = getpage.page + 1;
    String pageno = "";
    pageno = Integer.toString(getpage.page);
    EditField1.setText(pageno);
    The goal is to have EditField display how many times I've pressed the button. What I have works for me for the time being, but it doesn't count. It'll take the first declaration of the variable, add one to it, and display that. Any pointers?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    Is this included when you press the button:
    Code:
    getPage getpage = new getPage();
    It will make a new page object, having its own variable, and overwrite what you had in previous call/button-press.

    Any reason you have the variable in its own class?
    Maybe you should make it static (class variable). then you can call it directly with:
    Code:
    getPage.page
    without the need to make a new instance of the object first.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    73
    Rep Power
    10
    Originally Posted by MrFujin
    Is this included when you press the button:
    Code:
    getPage getpage = new getPage();
    It will make a new page object, having its own variable, and overwrite what you had in previous call/button-press.

    Any reason you have the variable in its own class?
    Maybe you should make it static (class variable). then you can call it directly with:
    Code:
    getPage.page
    without the need to make a new instance of the object first.
    Thank you very much, cleared my problem right up

IMN logo majestic logo threadwatch logo seochat tools logo