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 12 of 12

Thread: calling a changing variable from another class

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default calling a changing variable from another class

    I have redrafted the problem so as to be conceptually simpler.

    I have two calsses, a main class and resources class.

    MAIN CLASS
    public class Main {
     
        private static Resources resource = new Resources();
        public static void main(String[] args) {
     
        System.out.println("Program initiating");
        resource.add(1);
             System.out.println(resource.Queue);
     
        }
     
    }


    RESOURCE CLASS
    public class Resources {
     
        public int Queue;
        public boolean status;
     
     
        public Resources()
        {
        Queue = 0;
        status = false;
        }
     
        public void add(int x)
        {
            System.out.println("y is \t" + "Queue here is ");
            int y = x + 3;
            while (y<20)
            {
            y++;
            Queue++;
            System.out.println(y + "\t" + Queue + "\t");
            }
            //return y;
        }
     
    }

    I want to see the Queue variable in the resources class being changed as it loops. However i would like to do this by calling the Queue variable in the Resources class from the main class. Currently, when i do this (through System.out.println(resource.Queue); in the Main class), i am only being returned with the final number in Queue after all the loop has been done.

    Is there a way to implement this from the main class?
    thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calling a changing variable from another class

    Why do you need to do that?

    You could call the Resources code from an instance of Main (instead of just the main method), passing in a reference to this instance of Main. Then from your Resources code, call a setter in Main whenever it changes. Or you could use static variables.

    But I repeat- why do you need to do that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: calling a changing variable from another class

    KevinWorkman,

    thanks for your reply.

    Basically i have a main class that i use to print stuff that is changing in other classes. In one class, i have an interation that as time changes, one method adds a variable called queue and another method decreases it until a certain time passes. The aim is to make a simulation.

    When i print the results from the resources class, which contains all the said methods and the queue variable, i am returned with the queue at the end of the time period when i would like to have is returning the queue as it changes. This is much like the thing i am showing here. Can i print the Queue variable as the loop is changing? However i need to have this implementation in the main class.

    I hope it is somewhat clearer.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calling a changing variable from another class

    You aren't going to be able to implement this only in the Main class. You're only dealing with one thread here, so unless you add something to your Resources class, you aren't going to be able to access the variable when Resources is in the middle of a loop. And even if you did use multiple threads, without modifying the code in Resources, there'd be absolutely no way of guaranteeing that the behavior of main was any different than it is now.

    As I was hinting at earlier, this strikes me as a case of approaching the problem from the wrong direction.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: calling a changing variable from another class

    Thanks for the reply.

    Unfortunatley am bound to work with some code that i have not used and cannot change everything. Would it be possible to implement a method in the resources class that returns the value of Queue everytime it changes?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calling a changing variable from another class

    Quote Originally Posted by bondage View Post
    Thanks for the reply.

    Unfortunatley am bound to work with some code that i have not used and cannot change everything. Would it be possible to implement a method in the resources class that returns the value of Queue everytime it changes?
    Sort of. That was my first suggestion- pass the instance of Main into the Resources class, and call a setter in Main each time the value changes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: calling a changing variable from another class

    One final (maybe absurd) question. How do i pass the instance of Main into the Resources class?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calling a changing variable from another class

    Well, first you have to create an instance of Main. You do that exactly how you create an instance of Resources.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: calling a changing variable from another class

    But do you mean i create an instance of main in resources? i.e.

    public static Main mainclass = new Main();

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calling a changing variable from another class

    Quote Originally Posted by bondage View Post
    But do you mean i create an instance of main in resources? i.e.
    No, you're going to have one instance of Main- consider it the equivalent of your main method now. If you really want to pass a value back from another class (again, this seems like a bad design, but only you know your requirements), you then have to pass that instance of Main into Resources.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: calling a changing variable from another class

    yes but how do i concretely create an instance of main into resources, say from the exmaple i have posted in the first post of this thread?

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: calling a changing variable from another class

    Quote Originally Posted by bondage View Post
    yes but how do i concretely create an instance of main into resources, say from the exmaple i have posted in the first post of this thread?
    What have you tried? I already told you that you do it exactly the same way you create the Resources instance. Try something and post your attempt.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JButton Auto-changing Reference Variable
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 10:57 PM
  2. Changing Class File Content
    By xxcorrosionxx in forum Java Networking
    Replies: 2
    Last Post: January 18th, 2011, 07:46 AM
  3. about calling sub class
    By pokuri in forum Object Oriented Programming
    Replies: 3
    Last Post: January 11th, 2011, 03:30 PM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM