Question: I'm programming a big game in Java and I'm trying to optimize the code but also to keep the code neat and well organized. Now I am unsure if I should use the public static field of single classes that have a couple of variables that are used by a lot of instances. 

For example the class camera has an x and y position that define what part of the map the user is looking at and what needs to be drawn to the screen. Currently I'm benchmarking with 50 000 units and I have the following options to draw them. 

1: Store a reference to the instance of the camera in each unit and call getX() and getY() when it should be drawn:

```
public void paint()   
{   
   paint(x - camera.getX(), y - camera.getY());  
}  

```

2: Supply the coordinates of the camera as arguments to each unit when it should be drawn:

```
public void paint(int cameraX, int cameraY)  
{   
   paint(x - cameraX, y - cameraY);  
}

```

3: Make the x and y variables of the camera class static:

```
public void paint()   
{   
   paint(x - Camera.x, y - Camera.y);  
}

```

I'm interested as what is generally seen as the best solution and if it affects performance. Perhaps there are more ways to do this I haven't thought of yet? 

Thanks!

Answer: 
You'll have to benchmark in your context to be sure but here is my experience:

* Passing parameters to functions is generally the fastest if you have a small number of parameters that will be used a lot in an inner loop. This is because the JIT will assign these to registers
* Access to public static member variables is also pretty fast, only issues to watch out for are potentially concurrency issues (e.g. might the engine change the camera position while you are in the middle of rendering a frame?)
* Access via an instance reference is slowest, but still fast enough to be usable in a real time game. It helps if your class is final and the accessor methods are inlined of course.
In all programming languages, it is always recommended to use get/set methods for both static and instance members. This allows to change internal implementations transparently for a client code, if this is necessary. However, sometimes these rules are broken, if performance is very important.