Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Using static variables


    kamilo
     Share

    Recommended Posts

    I have gotten used to declaring static variables on top off the class to avoid creating new variables inside of a method (int i = 0). Because i heard new objects created is bad for cpu usage etc/garbage collector.

    example:

    private String getHighestHealingFood() {
    		healthRestored = 0; // Declared on top of the class
    		name = ""; // Declared on top of the class
    		for (Item item : getInventory().all(getNormalFilter())) {
    			for (Map.Entry<String, Integer> entry : getFoodMap().entrySet()) {
    				if (item.getName().contains(entry.getKey()) && entry.getValue() > healthRestored) {
    					healthRestored = entry.getValue();
    					name = entry.getKey();
    				}
    			}
    		}
    		return name;
    	}

    as you can see in the above method instead of initializing variables inside the method; i have initialized the variables outside (static variables); so no new ones need to be created and that makes a code a little more efficient; cuz of garbage collector etc

    But from some of the codes i've been reading on the forums; i am beginning to wonder why all the master scripters don't already do this, and it is a little more efficient

    even @Articron in his walkTo(); method: 

    As you can see in his inLoop() method he is initializing the variable Tile, Dest; every time the loop runs. So i am wondering why is he doing that if creating new variables everytime he runs the onLoop instead of initializing them at the top; then they would just change inside loop without creating new objects?

    So you can see why i am confused, since he is a master scripter and he isn't even doing that

    am i doing something wrong? i want to become a better scripter so i want to learn the CORRECT practise ofcourse.

    please guide me

     

    Link to comment
    Share on other sites

    So, when is it right to use static variables, because i am using them everywhere so no new objects are created everytime the code is ran; so if my script is a really long one all this pre=declared static variables should save SOME memory etc..

    Link to comment
    Share on other sites

    Yes you are correct that you should declare the variables at a global level and then use when needed, otherwise the variable gets initialised on each loop which is indeed inefficient. I guess the notice is negligible in some scripts and I'm sure some would argue it is negligible over all - I guess it is more of a 'good habit' more than an requirement. 

    I'd definitely get into the habit if you can though - it will only improve your scripts!

    Link to comment
    Share on other sites

    5 hours ago, StonedFarmer said:

    Yes you are correct that you should declare the variables at a global level and then use when needed, otherwise the variable gets initialised on each loop which is indeed inefficient. I guess the notice is negligible in some scripts and I'm sure some would argue it is negligible over all - I guess it is more of a 'good habit' more than an requirement. 

    I'd definitely get into the habit if you can though - it will only improve your scripts!

    Oh thank you! i thought i was doing something wrong by globally initializing static variables since i didn't see anyone else using them. if its a good practise, i'll definitely keep it up!

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.