sudoinit6 Posted March 23, 2018 Share Posted March 23, 2018 I know in Java there is no such thing as a global variable but I am looking for something similar. I am writing a script that does several things in 4 areas. I need a way to update a variable to say "section one is done, move on to section two" and then in my other classes be able to check the state of this variable. Can someone point me in a direction? Quote Link to comment Share on other sites More sharing options...
d0zza Posted March 23, 2018 Share Posted March 23, 2018 Make a Util class or something similar, either use a public variable there or use getters and setters for the variable and pass the instance of the Util object into all of your classes. 1 Quote Link to comment Share on other sites More sharing options...
Juggles Posted March 23, 2018 Share Posted March 23, 2018 If I am understanding correctly, why not just use booleans? Boolean hasBanked Then once it has banked, set it true and move on to the next step? Quote Link to comment Share on other sites More sharing options...
andrewboss Posted March 23, 2018 Share Posted March 23, 2018 24 minutes ago, Juggles said: If I am understanding correctly, why not just use booleans? Boolean hasBanked Then once it has banked, set it true and move on to the next step? That doesn't make sense.. Quote Link to comment Share on other sites More sharing options...
Juggles Posted March 23, 2018 Share Posted March 23, 2018 (edited) 1 minute ago, andrewboss said: That doesn't make sense.. How does it not make sense? start all booleans false, once it completes one thing, that boolean is true and moves onto next step until that one is true and continues. He has 4 steps he wants to accomplish so he can use 4 booleans and after each one is completed set it true Edited March 23, 2018 by Juggles Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted March 23, 2018 Author Share Posted March 23, 2018 29 minutes ago, Juggles said: If I am understanding correctly, why not just use booleans? Boolean hasBanked Then once it has banked, set it true and move on to the next step? That's exactly what I was looking for. You are awesome as always. I have often said that this farming thing would be a lot easier if I knew Java! 2 Quote Link to comment Share on other sites More sharing options...
Vichy Posted March 24, 2018 Share Posted March 24, 2018 (edited) I always optimize my code to less use primitive variables when possible. public enum States { BANKED, WHATEVER } public static States state; private static void transitionState(States state) { Class.states = state; } public static States getCurrentState() { return Class.state; } Something along these lines could also be used (quickly made it up obviously) Edited February 14, 2019 by Vichy Quote Link to comment Share on other sites More sharing options...
dreameo Posted March 24, 2018 Share Posted March 24, 2018 global variables do exist lol. Seems like you're doing everything inside of a single class. You just need to init the variable within the class and any object or method can access it. Quote Link to comment Share on other sites More sharing options...
Chris Posted March 24, 2018 Share Posted March 24, 2018 1 hour ago, dreameo said: global variables do exist lol. Seems like you're doing everything inside of a single class. You just need to init the variable within the class and any object or method can access it. just go static 1 Quote Link to comment Share on other sites More sharing options...
dreameo Posted March 24, 2018 Share Posted March 24, 2018 1 hour ago, Chris said: just go static then you force methods to be static loop would look like this since you couldn't use any of your static methods inside onLoop(){ Main.cut() Main.bank() ... } Quote Link to comment Share on other sites More sharing options...
Team Cape Posted March 24, 2018 Share Posted March 24, 2018 'public' for global variables related to an instance of a class 'public static' for global variables related to no particular instance Quote Link to comment Share on other sites More sharing options...
Chris Posted March 24, 2018 Share Posted March 24, 2018 12 hours ago, dreameo said: then you force methods to be static loop would look like this since you couldn't use any of your static methods inside onLoop(){ Main.cut() Main.bank() ... } 12 hours ago, Team Cape said: 'public' for global variables related to an instance of a class 'public static' for global variables related to no particular instance 1 Quote Link to comment Share on other sites More sharing options...
dreameo Posted March 24, 2018 Share Posted March 24, 2018 54 minutes ago, Chris said: 15 hours ago, dreameo said: global variables do exist lol. 13 hours ago, Team Cape said: 'public' for global variables related to an instance of a class 14 hours ago, Chris said: just go static 15 hours ago, dreameo said: Seems like you're doing everything inside of a single class. You just need to init the variable within the class and any object or method can access it. 13 hours ago, Team Cape said: 'public' for global variables related to an instance of a class 'public static' for global variables related to no particular instance 13 hours ago, dreameo said: then you force methods to be static loop would look like this since you couldn't use any of your static methods inside onLoop(){ Main.cut() Main.bank() ... } 1 Quote Link to comment Share on other sites More sharing options...
sudoinit6 Posted March 24, 2018 Author Share Posted March 24, 2018 Thanks for all the good advice. I am not very sophisticated as a coder so the Booleans will suit my needs for now. Baby steps. I do appreciate the replies though, this community is great. Quote Link to comment Share on other sites More sharing options...
LoudPacks Posted March 24, 2018 Share Posted March 24, 2018 14 hours ago, dreameo said: then you force methods to be static loop would look like this since you couldn't use any of your static methods inside onLoop(){ Main.cut() Main.bank() ... } You can reference static variables from non-static instances. 1 Quote Link to comment Share on other sites More sharing options...