March 23, 20187 yr 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?
March 23, 20187 yr 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.
March 23, 20187 yr 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?
March 23, 20187 yr 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..
March 23, 20187 yr 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, 20187 yr by Juggles
March 23, 20187 yr Author 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!
March 24, 20187 yr 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, 20196 yr by Vichy
March 24, 20187 yr 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.
March 24, 20187 yr 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
March 24, 20187 yr 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() ... }
March 24, 20187 yr 'public' for global variables related to an instance of a class 'public static' for global variables related to no particular instance
March 24, 20187 yr 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
March 24, 20187 yr 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() ... }
March 24, 20187 yr Author 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.
March 24, 20187 yr 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.
Create an account or sign in to comment