![]() |
So What Is Vosplitzel? |
![]() |
So What Is an Object? |
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 |
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? |
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? |
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? |
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 |
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