I was wondering how it is possible to get the value of a variable/boolean that is inside of a loop in a different class.
I would have a variable in one class and want get that in another one:
Class1:
public void mainLoop()
{
while(!Display.isCloseRequested)
{
frames++
if(frames == 200)
{
key = 5
run = false;
}
if(frames == 400)
{
key = 10
run = true;
}
}
}
and in my other Class2 I want to acess the changed varibles:
public Class2()
{
public void printVariables(int key)
{
if(key == 5) { System.out.println("KEY 5"); }
if(key == 10) { System.out.println("KEY 10"); }
if(run == false) { System.out.println("RUN FALSE"); }
if(run == true) { System.out.println("RUN TRUE"); }
}
}
How?
Thanks for any Help!
Add it as a parameter to the method:
public Class2()
{
public void printVariables(int key)
{
if(key == 5) { System.out.println("KEY 5"); }
if(key == 10) { System.out.println("KEY 10"); }
if(run == false) { System.out.println("RUN FALSE"); }
if(run == true) { System.out.println("RUN TRUE"); }
}
}
And then call that method with an instance of the class:
public void mainLoop()
{
Class2 cls2 = new Class2();
while(someCondition == true)
{
frames++
if(frames == 200)
{
key = 5
run = false;
}
if(frames == 400)
{
key = 10
run = true;
}
cls2.printVariables(key);
}
}
Or, if you can, make the method static and call it statically (ie Class2.printVariables(key)
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.