The Undefeated Posted April 3, 2017 Share Posted April 3, 2017 I can't switch any booleans from another class. public class Main { public boolean hans = false; public int onLoop() { Processing p = new Processing(); p.exchangeContext(getBot(); log("Hans is" + hans); p.Loop(); return 500; } } public class Processing { public void Loop() { Main m = new Main(); m.exchangeContext(getBot()); log("Switching hans to true)" m.hans = true; } } As soon as I use this loop, the log will say: [INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true [INFO][Bot #1][04/03 04:14:29 PM]: hans is false Pretty sure it's some dumb mistake but I can't get it. 1 Quote Link to comment Share on other sites More sharing options...
harrypotter Posted April 3, 2017 Share Posted April 3, 2017 (edited) 13 minutes ago, The Undefeated said: I can't switch any booleans from another class. public class Main { public boolean hans = false; public int onLoop() { Processing p = new Processing(); p.exchangeContext(getBot(); log("Hans is" + hans); p.Loop(); return 500; } } public class Processing { public void Loop() { Main m = new Main(); m.exchangeContext(getBot()); log("Switching hans to true)" m.hans = true; } } As soon as I use this loop, the log will say: [INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true [INFO][Bot #1][04/03 04:14:29 PM]: hans is false Pretty sure it's some dumb mistake but I can't get it. Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value? I.e: public class Main { public boolean hans = false; public void setHansValue(boolean val) { this.hans = val; } public int onLoop() { Processing p = new Processing(); p.exchangeContext(getBot(); log("Hans is" + hans); p.Loop(); return 500; } } public class Processing { public void Loop() { m.exchangeContext(getBot()); log("Switching hans to true)" Main.setHansValue(true); } } Edited April 3, 2017 by harrypotter 1 Quote Link to comment Share on other sites More sharing options...
Final Posted April 3, 2017 Share Posted April 3, 2017 (edited) 1 hour ago, The Undefeated said: I can't switch any booleans from another class. public class Main { public boolean hans = false; public int onLoop() { Processing p = new Processing(); p.exchangeContext(getBot(); log("Hans is" + hans); p.Loop(); return 500; } } public class Processing { public void Loop() { Main m = new Main(); m.exchangeContext(getBot()); log("Switching hans to true)" m.hans = true; } } As soon as I use this loop, the log will say: [INFO][Bot #1][04/03 04:14:29 PM]: Switching hans to true [INFO][Bot #1][04/03 04:14:29 PM]: hans is false Pretty sure it's some dumb mistake but I can't get it. You are changing the boolean value in one instance of the class and outputting the boolean value from another. They aren't the same value. The solution is to use getters and setters to change fields within a class. Edited April 3, 2017 by Final 2 Quote Link to comment Share on other sites More sharing options...
The Undefeated Posted April 3, 2017 Author Share Posted April 3, 2017 8 minutes ago, Final said: You are changing the boolean value in one class and outputting the boolean value from another. They aren't the same value. 11 minutes ago, harrypotter said: Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value? I.e: public class Main { public boolean hans = false; public void setHansValue(boolean val) { this.hans = val; } public int onLoop() { Processing p = new Processing(); p.exchangeContext(getBot(); log("Hans is" + hans); p.Loop(); return 500; } } public class Processing { public void Loop() { m.exchangeContext(getBot()); log("Switching hans to true)" Main.setHansValue(true); } } Some Java basics I completely forget sometimes, I am ashamed. Thanks both. Quote Link to comment Share on other sites More sharing options...
Vilius Posted April 3, 2017 Share Posted April 3, 2017 (edited) 14 minutes ago, Final said: You are changing the boolean value in one instance of the class and outputting the boolean value from another. They aren't the same value. The solution is to use getters and setters to change fields within a class. 17 minutes ago, harrypotter said: Make hans static (However I'm sure that's a bad idea unless they're final) or in your main class have a method that's responsible for updating the value? I.e: public class Main { public boolean hans = false; public void setHansValue(boolean val) { this.hans = val; } public int onLoop() { Processing p = new Processing(); p.exchangeContext(getBot(); log("Hans is" + hans); p.Loop(); return 500; } } public class Processing { public void Loop() { m.exchangeContext(getBot()); log("Switching hans to true)" Main.setHansValue(true); } } http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value http://softwareengineering.stackexchange.com/questions/21802/when-are-getters-and-setters-justified http://stackoverflow.com/questions/1568091/why-use-getters-and-setters http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil Read pls Edited April 3, 2017 by Vilius Quote Link to comment Share on other sites More sharing options...