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
Code :
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
Code :
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
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?
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.
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.
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?
Re: calling a changing variable from another class
Quote:
Originally Posted by
bondage
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.
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?
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.
Re: calling a changing variable from another class
But do you mean i create an instance of main in resources? i.e.
Quote:
public static Main mainclass = new Main();
Re: calling a changing variable from another class
Quote:
Originally Posted by
bondage
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.
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?
Re: calling a changing variable from another class
Quote:
Originally Posted by
bondage
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.