Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Getting primitive variable values from another class

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Getting primitive variable values from another class

    Sorry if this question has already been answered in a different thread but I can't find a solution on this site that suits my problem.

    I am making an Android app and in one class, "Menu.java", I have a 'int' value which starts off being defined as 1 but gets changed during the actions of the class as it is actually a user input and ends up as a certain value and ceases to change. This class, "Menu.java", is then put into the background and a new class, "NumScreen.java", is started. In "NumScreen.java" the final value of the 'int' in "Menu.java" is needed in a number of different processes as a constant value.

    My question is: How do I get that last value of the 'int' from "Menu.java" to "NumScreen.java" so I can use it in the processes that follow in "NumScreen.java"?

    Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting primitive variable values from another class

    Add a getter method in the Menu class that returns the value. Call that method when you want the value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Giacomo (January 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Getting primitive variable values from another class

    Thought this was working but in my case its not... I've defined the variable in Menu but then changed it upon the clicking of a button. Where do I put the code for the getter in order for the getter to return the new value of the int when it is used in NumScreen?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting primitive variable values from another class

    The getter method should be in the class that has the variable whose value is to be returned.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting primitive variable values from another class

    Hii,
    1.define the variable with protected or public in Menu.java.

    2.Extend the Menu.java class in NumScreen.java.
    3.use what ever variables u want from Menu.java by super.variable in NumScreen.

    if ur value is changing in Menu.java.Store all values in array and acces the array.
    Ex:
    public class Menu{

    public or protected int[] var;
    }

    public class NumScreen extends Menu{

    NumScreen(int[] myvar){
    super.var=myvar;
    }

    }


    I think this will help,if there are any mistakes ,please revert back.
    thank u.

  7. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Getting primitive variable values from another class

    Quote Originally Posted by Norm View Post
    The getter method should be in the class that has the variable whose value is to be returned.
    I have put the get method in the Menu class not in any subclass. I have set the default value of the variable to 1 but there is a subsection namely the 'public void onClick(view v){.........}' where the value of the variable is changed to something completely different. I have put the get method outside this subsection but within the class Menu.java. I have then used the get method in NumScreen.java at which point it returns the value 1 when it should have been changed by user input due to the code in the subsection. I'm not sure if the problem lies with the get method/its position or if the variable isn't being edited by the subsection.

    Is it sufficient to have this code within the button click subsection:

    EditText time_input = (EditText) findViewById(R.id.time_input);
    time = Integer.parseInt(time_input.getText().toString());

    and expect it to change the value of the 'time' integer throughout the entire class or is there something more I must do for the get method to return this changed 'time' value?

    Thanks

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting primitive variable values from another class

    change the value of the 'time' integer throughout the entire class
    If time is a class variable, when its value changes, the whole class should see the new value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Giacomo (January 27th, 2013)

  10. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Getting primitive variable values from another class

    Thanks - that's very helpful I had forgotten to use static int time; so it was only changing in the subsection.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting primitive variable values from another class

    If you have more than one instance of a class and change the value of a variable in one instance, the value will NOT be changed in any other instances,
    UNLESS the variable is static. Then all the instances of the class will share the ONE static variable.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Getting primitive variable values from another class

    Thanks as you might be able to tell, I've only just started java - this has been very helpful.

Similar Threads

  1. Using variable in different class
    By ICEPower in forum Object Oriented Programming
    Replies: 13
    Last Post: May 12th, 2012, 12:32 PM
  2. [SOLVED] Using values set in one class in another
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 23rd, 2011, 10:34 PM
  3. Keeping Variable Values
    By The_Mexican in forum Java Theory & Questions
    Replies: 6
    Last Post: November 27th, 2010, 04:02 PM
  4. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 03:10 PM
  5. How to share variable values amongst different classes?
    By igniteflow in forum Object Oriented Programming
    Replies: 8
    Last Post: August 20th, 2009, 08:53 AM

Tags for this Thread