Herd declares class variables - I don't understand why

Post Reply
User avatar
gorzak
Posts: 18
Joined: Fri May 18, 2007 2:25 pm

Herd declares class variables - I don't understand why

Post by gorzak »

I'm trying to understand what the static modifier does for the example bot Herd provided in the SDK. From what I do understand, each instance has it's own memory space, thus nullifying the use of static members between seperate instances. This makes it seem to me that the declared static has no effect. What am I missing here?

I am using this game and these examples to teach myself how to program. My bot is my first program ever, and java is the first language I am learning. Because of that I may be overanalyzing things. Since I am just learning though, I am trying to understand the effects of nearly everything.

When I encounted the " static " modifier reading the java tutorials, I thought of plenty of neat ways to use it. My first thought was making a map. Then i thought about arrays of where the units are. Then I realized if it worked, it would essentially render the sensor limits pointless. I was sure that the limit would be enforced somehow, so i doublechecked the documentation. Under the radio section I found:
Each bot exists in its own VM, and cannot access memory of another bot directly.
Then I remembered that one of the example bots uses the static modifier. I double checked, and sure enough it has :

Code: Select all

	public static final int CITY = 0;
	public static final int GRUNT = 1;
	public static final int HOVERCRAFT = 2;
	public static final int ARTIL = 3;

To prove conclusively that the bots dont use class variables, I wrote some code and did some testing. Sure enough, despite me declaring the destination as a class variable, each bot went to its own seperate destination. When I checked the "run natively" option, it demonstrated that I did code it correctly, all of the bots converged on the same location.

Now I am really confused. I understand that static members could be useful to inner or packaged classes, but if there is a limit of one instance of the class per environment (I hope thats the right term) like there is in this game, I still don't understand what benefit declaring a member static could confer.

I also searched on the web, but that gave me more questions than answers. I got a few hits that static final primatives are constants and therefore cut out during compile time. I got a few saying that you only need final to declare constants, not static. Another said that constants weren't supported by the language. Another suggested that some compilers do things others don't. I probed technical documents to the best of my abilities, and I am sure I learned some things from it, but not the answer to my question.



P.S. This is the code I used to answer my first question on if static variables worked if class instances were loaded in seperate VMs. The difference between running natively and in the modified VM is quite noticable :D . If anyone as newb as I am now wants a demonstration, feel free.

Code: Select all

import java.util.Random;
public class Testing {
	public int orderType = 1; // always a move order
	public double destX, destY; // the move order will use these fields to communicate a destination
	Random r = new Random(); // help us randomize our actions
	public static double teamX,teamY;
	{
		Testing.teamX = 25 ;
		Testing.teamY = 25 ;
	}
	public Object think(double dx, double dy, double x, double y, boolean moving, int terrain,
    		int ourID, int ourType, double hp, double maxHP, double range, double time,
    		double[] objX, double[] objY, int[] objID, int[] objFaction, int[] objType, int[][] incomingRadio) 
	{
		
		
		if (moving ){ //if we are already moving
			if (destX == Testing.teamX && destY == Testing.teamY) return null; // no new orders if we are headed to the team goal
			destX = Testing.teamX; 
			destY = Testing.teamY;
			return this;// get with the team
			
		}
		if (!moving && ourType != 0){// if we arent moving and we arent a city
			if (destX == Testing.teamX && destY == Testing.teamY){// if we have arrived at the destination
				Testing.teamX = r.nextDouble() * (dx); 
				Testing.teamY = r.nextDouble() * (dy);
				return this;// choose random destination for the team
			}
			destX = Testing.teamX; 
			destY = Testing.teamY;
			return this;// get with the team
		}
		
		
		return this;
	}
	
	public int build(double dx, double dy, double x, double y, int terrain, int id, int buildItem,
		double hp, double maxHP, double time,
		double[] objX, double[] objY, int[] objID, int[] objFaction, int[] objType, int[][] incomingRadio) 
	{
		if (buildItem != 0) return 0; // if we're currently building, keep at it
		return 1 + r.nextInt(3); // choose randomly
	}
}
P.P.S. If you viewed this post before I read and applied the knowledge from How To Ask Questions The Smart Way , I apologize.
Captain Segfault
Posts: 67
Joined: Sat May 05, 2007 6:11 pm
Location: San Carlos, CA
Contact:

Re: Herd declares class variables - I don't understand why

Post by Captain Segfault »

[quote="gorzak"]
Then I remembered that one of the example bots uses the static modifier. I double checked, and sure enough it has :

Code: Select all

	public static final int CITY = 0;
	public static final int GRUNT = 1;
	public static final int HOVERCRAFT = 2;
	public static final int ARTIL = 3;
These are constants. The point of the "static final" is so that they don't get any memory allocated per object (static) and they aren't going to change (final). With that the compiler can optimize them out, I think.

I assume bots will see the initial value but not any changes other bots make -- so you should probably not have any static variables that aren't final, since they'll behave differently in native.
User avatar
adum
Posts: 392
Joined: Thu Apr 19, 2007 12:49 pm
Contact:

Post by adum »

hi gorzak. you are quite correct: the static modifier in the Herd example doesn't do anything under the HackJVM. so why did i write it? basically, this is a standard Java programming technique: declaring constants as 'static final'. it's final so you can't change it by accident, and by making it static, you don't have to create a new instance of the variable for each class instance that gets created, which would waste space. also, since it's static, other classes can access the value without having to create an instance of the class, which is very useful.

cheers,
adum
User avatar
gorzak
Posts: 18
Joined: Fri May 18, 2007 2:25 pm

thanks

Post by gorzak »

That's good to know. I'll go ahead and start using it myself now to get in the habit of it. I have zero previous experience programming so anything done by convention not mentioned in Sun's java tutorial is going to escape me. Thanks for taking the time out to consider and respond to my newbish question.
Post Reply