Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By b4viral

Thread: returning variable from different class

  1. #1
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default returning variable from different class

    Not exactly sure how to take a variable from one class and use it in a different class.
    tried something like this but it doesnt seem to work.
    Java Code:
    public class Score{
        
        int score = 0;
        
        public Score(int x){
            score = score + x;
        }
        
        public int Finish(){
            return score;
        }
        
    }
    then in my other class:
    Java Code:
        public void SetScore(){
            int score;
            Score value = new Score(0);
            value.Finish();
            jLabel4.setText(""+score);
        }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: returning variable from different class

    you can say:

    if you mad score static you can say: Thanks Jim for the correction

    score = Score.score since your score variable is available to every Class it the package.

    if your variable isnt private the rule is:

    variable to be set = ClassName.variableName;

    if your score variable in the Score Class was private you would need a Class reference and a getter.
    Last edited by monkeyjr97; 05-13-2013 at 09:34 PM.

  3. #3
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: returning variable from different class

    public void SetScore(){
    int score; [NOTE: better you use some different name here just to avoid confusion :)]
    Score value = new Score(0);
    value.Finish();<------------------------------------ Problem is here, store the value returned by the method into appropriate variable (score here)
    jLabel4.setText(""+score);
    }

    a suggestion : we should not use ambiguous names of variables which may lead us/others to confusion, better we follow proper naming conventions.
    heatblazer likes this.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: returning variable from different class

    Quote Originally Posted by monkeyjr97 View Post
    you can say:

    score = Score.score since your score variable is available to every Class it the package.
    The variable score is not declared static so that type of access won't work. An instance of the Score class is required to access the variable.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: returning variable from different class

    Thank you it worked; now i got another question however.. why is this not working?
    Java Code:
        public Score(int x){
            score = score + x;
        }
    so say i want to add 10 to the score from my other method i would do
    Java Code:
    Score score = new Score(10);
    then it should plug 10 in for x, and add it to the variable score.
    but instead it is just replacing it for that value, so if it was initially 20 it is now 10..
    any idea why?

  6. #6
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: returning variable from different class

    that will create a new score class, if you want to just add 10 to the current score:

    [code]

    public void addToScore(int toAdd) {

    score = score + toAdd;

    }


    [code]

  7. #7
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: returning variable from different class

    Dont understand really what youre getting at..

    changed it so its like this:
    Java Code:
        public Score(){
        }
        public void addToScore(int toAdd) {
        score = score + toAdd;
        System.out.println(score);
    }
    Java Code:
            Score score = new Score();
            score.addToScore(10);
    looks nicer but still it just takes over the score variable, so whatever i add to it now becomes scores value..
    if i did:
    Java Code:
    score.addToScore(10);
    score.addToScore(15);
    it should be 25 but its only 15..
    Last edited by leeroijenkiins; 05-13-2013 at 09:55 PM.

  8. #8
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: returning variable from different class

    Hmm...

    try this instead:

    Java Code:
    score += toAdd;

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: returning variable from different class

    At this point, it might be helpful to show all of your relevant code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: returning variable from different class

    Quote Originally Posted by monkeyjr97 View Post
    Hmm...

    try this instead:

    Java Code:
    score += toAdd;
    Does the same thing :/

  11. #11
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: returning variable from different class

    Quote Originally Posted by jim829 View Post
    At this point, it might be helpful to show all of your relevant code.

    Regards,
    Jim
    well heres my whole score class:
    Java Code:
    public class Score{
        
        int score = 0;
        
        public Score(){
        }
        
        public void addToScore(int toAdd) {
        score += toAdd;
        System.out.println(score);
    }   
    }
    then in all my other classes there is 4 options; 1 in correct, you pick the correct 1 and it adds to your score like this:
    Java Code:
            Score score = new Score();
            score.addToScore(10);
    then at the end i want to show the score so:
    Java Code:
        public void SetScore(){
            Score value = new Score();
            score = value.score;
            jLabel4.setText(""+score);
        }
    In my score class however just with the print i can see its not adding to the score but simply changing it to the amount i add. I dont understand why

  12. #12
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: returning variable from different class

    put a method in Score that returns the score

    Java Code:
    public int getScore(){
    
    return score;
    
    }


    then :

    Java Code:
    public void SetScore(){
        Score value = new Score();
        value.addToScore(15);
        value.addToScore(20);
        score = value.getScore();
        jLabel4.setText(""+score);
    }
    score should now be 35


    Also i would define score in your constructor, just in case that is causing strange behavior (Not sure if it would or not)

    Java Code:
    int score;
         
        public Score(){
    
          score = 0;
        }
    Last edited by monkeyjr97; 05-13-2013 at 11:28 PM.

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: returning variable from different class

    There are lots of ways to accomplish what you want. This example uses forwarding methods to manipulate and retrieve a value from a different class.

    Java Code:
    public class TestScore {
       private Score scoreInstance;
       public TestScore() {
          scoreInstance = new Score();
       }
       public static void main(String[] args) {
          int curScore = 0;
          TestScore ts = new TestScore(); 
          System.out.println(curScore); // 0
          curScore = ts.addToScore(10);  
          System.out.println(curScore); // 10 
          curScore = ts.addToScore(15);  
          System.out.println(curScore); // 25
          curScore = ts.setScore(17);
          System.out.println(curScore); // 17
       }
    
       public int addToScore(int s) {
          return scoreInstance.addToScore(s);
       }
       public int setScore(int s) {
          return scoreInstance.setScore(s);
       }
       public int getScore() {
          return scoreInstance.getScore();
       }
    }
    
    class Score {
       private int score = 0;
       public int addToScore(int toAdd) {
          score += toAdd;
          return score;
       }
       public int setScore(int s) {
          score = s;
          return s;
       }
       public int getScore() {
          return score;
       }
    }
    You could also just use a reference to Score directly and access the methods that way. It all depends on what you want to accomplish and how you want to accomplish it.

    Regards,
    Jim
    Last edited by jim829; 05-14-2013 at 12:58 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: returning variable from different class

    @monkeyjr97

    I tried all of those and its still doing the same thing :(

    @jim829

    I see what you are saying and that would work if i was doing it the smart way; but you see im using netbeans and in there i find it more convenient to use mulitple JFrames instead of Panels.. anywho i got about 13 different classes, so im trying to use my score class to control the score throughout the program and then just print it in the final class.
    Im really not sure why it keeps resetting back to 0 after it is given a value..

    so it goes to question one and if they click the right answer:
    Java Code:
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            Question2 l = new Question2();
            l.setVisible(true);
            Score score = new Score();
            score.addToScore(10);
            dispose();
        }
    wrong answer just does this:
    Java Code:
            Question2 l = new Question2();
            l.setVisible(true);
            dispose();
    till the last question and then a final analysis frame comes up and displays the results:
    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            jLabel2.setVisible(true);
            jLabel4.setVisible(true);
            SetScore();
        }
    Java Code:
        public void SetScore(){
            Score value = new Score();
            score = value.getScore();
            jLabel4.setText(""+score);
        }
    Score class:
    Java Code:
    class Score {
       private int score;
       public Score(){
           score = 0;
       }
       public int addToScore(int toAdd) {
          score += toAdd;
          System.out.println(score);
          return score;
       }
       public int setScore(int s) {
          score = s;
          return s;
       }
       public int getScore() {
          return score;
       }
    }
    im really getting quite confused here

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: returning variable from different class

    In your actionListener you call SetScore. In SetScore you create a new instance and return the value. At that point, the value is 0. You must keep using the same instance if you want to keep getting a particular score. For each instance you create, there exists one unique score variable. Here is an example:

    Java Code:
    Score value = new Score();
    value.addToScore(10);
    value = new Score(); // access to old instance (and old score now gone)
    int score = value.getScore();  // here it is zero, not 10
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 06-27-2012, 02:55 PM
  2. Returning ResultSet from class
    By KingdomX in forum New To Java
    Replies: 10
    Last Post: 12-23-2011, 06:10 PM
  3. Replies: 5
    Last Post: 12-18-2011, 04:06 AM
  4. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  5. Replies: 5
    Last Post: 01-26-2011, 06:38 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
  •