Results 1 to 12 of 12
  1. #1
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Red face How to set a variable's value in one class, while having the value updated in another

    Hi all, I've got a certain instance in my program where an integer variable, let's call it 'x', is initialised in one part of a class, however the value is not present when the integer is called in another class.

    To explain further, I've got 2 classes - 'class1' and 'class2'. The variable is declared in 'class1' as 'x'. At some point in class1, I give 'x' a value, let's say 5.

    Now, when I create an object of 'class1' in 'class2', 'x' is automatically equal to 0. Why is it that after running class1, and then linking to class2, the value of 5 is not carried over onto the 'x' object in class2?

    I'm sorry if it's a bit confusing, I'll try to explain differently if it's not understandable. Basically I want to know how one can update values of a variable in one class, and have those values updated in another class when you call it (obviously while initialising it during run time).

    Thanks :)

  2. #2
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

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

    Default

    Your problem may be one of references. You are likely changing the value of x in one object of Class1 (classes begin with capital letters), and trying to check the value in another object of Class1. Changing the value in one object does not change it in another. A possible solution is to pass a reference from the first Class1 object to elsewhere in the program where you need it, rather than creating another and unrelated Class1 object. Another solution is to make "x" a static variable, but I recommend that you do this reluctantly, and only after studying your problem you are sure that x should in fact be static. A common newbie mistake is over-reliance on statics, and this will often bite the programmer in the end.

    Clear as mud?

  4. #4
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your problem may be one of references. You are likely changing the value of x in one object of Class1 (classes begin with capital letters), and trying to check the value in another object of Class1. Changing the value in one object does not change it in another. A possible solution is to pass a reference from the first Class1 object to elsewhere in the program where you need it, rather than creating another and unrelated Class1 object. Another solution is to make "x" a static variable, but I recommend that you do this reluctantly, and only after studying your problem you are sure that x should in fact be static. A common newbie mistake is over-reliance on statics, and this will often bite the programmer in the end.

    Clear as mud?
    I see where you're going with the first part, now that I think about it, I am infact creating a completely new instance of the class and hence cannot expect it to be updated there. How exactly could I refer to the class without creating a new object though? Inheritance possible?However I would be unable to do that as I am extending from JFrame already. Any ideas? :)

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

    Default

    Quote Originally Posted by SeanC View Post
    I see where you're going with the first part, now that I think about it, I am infact creating a completely new instance of the class and hence cannot expect it to be updated there. How exactly could I refer to the class without creating a new object though? Inheritance possible?However I would be unable to do that as I am extending from JFrame already. Any ideas? :)
    Yes, pass a reference of the desired object via a method or constructor parameter. For instance, say you have a class called ButtonPanel that extends JPanel that you want to pass into your JPanel; your GUI could have a ButtonPanel variable, say buttonPanel, and a method, say addButtonPanel(ButtonPanel buttonPanel), and this code could look something like so:

    Java Code:
    class MainGui extends JFrame {
      private ButtonPanel buttonPanel; // variable to hold the reference
    
      //... constructors and other methods....
    
      public void addButtonPanel(ButtonPanel buttonPanel) {
         this.buttonPanel = buttonPanel;
         // possibly add this component into the GUI here?
      }
    }

  6. #6
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yes, pass a reference of the desired object via a method or constructor parameter. For instance, say you have a class called ButtonPanel that extends JPanel that you want to pass into your JPanel; your GUI could have a ButtonPanel variable, say buttonPanel, and a method, say addButtonPanel(ButtonPanel buttonPanel), and this code could look something like so:

    Java Code:
    class MainGui extends JFrame {
      private ButtonPanel buttonPanel; // variable to hold the reference
    
      //... constructors and other methods....
    
      public void addButtonPanel(ButtonPanel buttonPanel) {
         this.buttonPanel = buttonPanel;
         // possibly add this component into the GUI here?
      }
    }
    Unfortunately I do not fully understand the code you wrote (which I greatly appreciate!). I'm not too sure what's going on in the method, and how it could help make a reference. Would you recommend this over just making the variable static?

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

    Default

    Quote Originally Posted by SeanC View Post
    Unfortunately I do not fully understand the code you wrote (which I greatly appreciate!). I'm not too sure what's going on in the method, and how it could help make a reference. Would you recommend this over just making the variable static?
    Yes, in fact I recommend doing this 1000 X over using static in this situation. My code above just shows a basic setter method by another name, and in fact I probably should have named the addButtonPanel method setButtonPanel instead. If you are not familiar with getter and setter methods (also known as accessor and mutator methods), I urge you to read your text book or other reference source on this.

    Best of luck!

  8. #8
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yes, in fact I recommend doing this 1000 X over using static in this situation. My code above just shows a basic setter method by another name, and in fact I probably should have named the addButtonPanel method setButtonPanel instead. If you are not familiar with getter and setter methods (also known as accessor and mutator methods), I urge you to read your text book or other reference source on this.

    Best of luck!
    Yes, I am infact familiar with getters and setters. From what I'm seeing, you've basically set a setter over there (for example 'setX'), so for me to call the object of the other class in relation to x, i should just call 'getX'?

    I tried making x static, and it worked. However, if you're saying that the latter is not a good programming technique, then I'll try it using getters and setters since ultimately I'm here to learn the 'proper' way!

    I appreciate the help :)

  9. #9
    bm0807 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    I think using "static" variable can solve your problem.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by bm0807 View Post
    I think using "static" variable can solve your problem.
    Probably not in a an OO compliant (nor practical) manner, however. And would probably create other problems.

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

    Default

    Quote Originally Posted by bm0807 View Post
    I think using "static" variable can solve your problem.
    This is absolutely the wrong advice for this situation, and if you had read the whole thread for this question above, you'd know this and wouldn't have given this bad advice. Please let's try not to mislead the newbies, OK?

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Unfortunately, in the above linked cross-post the OP claims to have done, exactly, that.

Similar Threads

  1. Replies: 14
    Last Post: 06-17-2010, 03:47 PM
  2. batch updated
    By swati.jyoti in forum JDBC
    Replies: 2
    Last Post: 06-09-2009, 11:33 PM
  3. Replies: 3
    Last Post: 04-24-2009, 06:28 PM
  4. Variable name determined by another variable's value
    By Lumpkabob in forum New To Java
    Replies: 5
    Last Post: 04-14-2009, 08:00 AM
  5. Java path variable's
    By Jordsk in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:20 AM

Tags for this Thread

Posting Permissions

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