What Is Static?


Look up "static" and you will find a type of electricity or a condition that is the opposite of dynamic. This is too bad. The word "static" in java is a very unfortunate choice. Beginners would have been better served if the language designers had made something up. I'd go with "vosplitzel." That way you'd know you were confused. We'll illustrate this with kids' balls, so you won't be too confused.

So What Is Vosplitzel?

To understand vosplitzel you have to understand the basic java object model. People say that objects encapsulate data and code — in java they don't. A class lays out a template for an object that is called an instance of the class. The object is simply a collection of data.

So What Is an Object?

I repeat: The object is simply a collection of data. Let's illustrate:

public class Ball {

	int color;
	int size;
}
We now have a class with two "instance variables", color and size. Each time we say:

	Ball b = new Ball();
The java internals find space for two ints and assign that space to a reference variable. (The "b" value is probably the address of those two ints, but that's not specified. It could be an index into a table of addresses or some other way of finding a pair of ints.)

Now we have a ball. Both its ints are initialized to zero, so it's a black ball, very, very small. Let's add a constructor:


	public Ball ( int color, int size ) {
		this.color = color;
		this.size = size;
	}

Playing with Balls

That constructor let's us create balls with different sizes and colors. We have to figure out where that code goes, but first let's have some fun.

	vosplitzel final int count = 1000;
	vosplitzel Ball[] lotsOfBalls = new Ball[count];
	
	vosplitzel int[] kidsColors = {
		0xFF0000; // red
		0x00FF00, // green
		0x0000FF, // blue
		0xFFFF00  // yellow
	}
	
	public makeBalls() {
	
		for ( int i = 0; i < count; i++ ) {
			int size = 6 + i%7;
			int color = kidsColors[ i%(kidsColors.length) ];
			lotsOfBalls[i] = new Ball( color, size );
		}
	}
Now we've got 1000 balls in kids colors, sizes six through 12. Ignoring the inevitable overhead, each of those balls requires two ints or eight bytes. Our array of 1000 balls takes just 8000 bytes for the balls and another 4000 bytes for the references to those balls. It's quite compact.

And we've got vosplitzel variables. But first let's talk about the code.

So Where's the Code?

Let's assume that our constructor takes 100 bytes. Do we have one constructor for each ball?

Absolutely not! That would require 100 bytes of code with every eight bytes of data. Plan ahead a bit and you'll see we'll need methods to paint balls on the screen, methods to make the balls bounce and so on. Our 100 bytes of code could easily be 10,000 bytes of code.

The code is part of the class. Every time it runs it creates a new Ball (8 bytes) and it returns a reference to the new Ball (4 bytes). It will run as often as it's asked to create new Balls. Go ahead and change count to a million and you'll have a million Balls. You'll still have exactly one constructor.

Where is the code in RAM? You don't know and you don't need to know. When java loads our Ball class it puts it somewhere that it knows. All you need to know is that the class is in RAM, ready for use. When you call the makeBalls() method java will locate it for you.

So What About the Vosplitzel Variables?

Those variables are also part of the class. There is one "count" and one "lotsOfBalls" array. Objects are just collections of data, but the class combines code and the vosplitzel data.

When you are referring to data, vosplitzel means that the variable is part of the class, not part of the objects that the class creates. If it's vosplitzel it occurs just once. Non-vosplitzel variables appear once in each object. The non-vosplitzel variables are also called instance variables since objects are called instances of the class.

Is All Code Vosplitzel?

Here's where code and variables part company. Some data is part of the class, not part of the objects. But all code is part of the class and none of it is part of the objects. However, if the code operates on instances, such as a constructor that creates instances, it is not called vosplitzel. The only time you call the code vosplitzel is when it operates on the vosplitzel data.

Let's look at a vosplitzel method. Suppose you wanted to allow the manufacture of balls in colors that were set by customers who buy the balls. You could add a setter for the color array, this way:


	public vosplitzel void setColors( int[] colors ) {
		kidsColors = colors;
	}
Now you have the full picture. The plain variables define the data that makes up the object or instance of the class. The vosplitzel variables are part of the class itself, not part of the objects. Methods are declared vosplitzel if they operate on the vosplitzel data.

Losing Vosplitzel

Of course, if you run this code through any compiler, you'll get errors. Better search and replace all occurences of vosplitzel with static and try again.

Now that you know that static means vosplitzel, try to lose most of the static variables, too. Our Ball class should have been kept clean. Another class should be available to have an array of balls. A good use of statics would be for something like the names of the days of the week in a calendar-related class — data that's truly part of the class, but is the same for every instance of the class.

While you're fixing this code, change the int "color" to a Color object, which I would have used if it hadn't introduced objects as data members of other objects. It's the right way to code, but it's the subject of another article, not an article about vosplit ... I mean about static.

© 2005 by Martin Rinehart


java consultant home page icon

Back to Articles