Every Western town has a few key ingredients: stables, saloons, sheriffs, and a couple troublemakers. A standard town would have three stables and be located West of the Mississippi sometime around 1850. |
This description of a Western town, while not very profound, did two important things: 1) It established the key ingredients for a Western Town and 2) It gave several values that might occur in a default Western Town. (Any more than three stables and the place starts to stink)
|
It may seem that Eunice has a strange way of writing, but to her, it's straightforward. The first line on the page states that Eunice is defining a Western town. You may have noticed that when Eunice needs to write two words, she won't use a space. There is a reason for this. If she puts spaces in, her editor in New York, a stickler for details, gets confused.
The next step, logically, is to then declare what sorts of variables (those things which define how the town looks) that a class of Western towns might have. Sheriffs, stables, troublemakers, etc. That being said, all that was left to do was to construct a sample town, with default values for her variables. Whew!
public class WesternTown { int stables; int saloons; int sheriffs; int troublemakers; String location; int time; public WesternTown() { stables = 3; location = "Western America"; time = 1850; } } |
In making her Western Town, Eunice is defining a class, not the town itself (that would be an object). A class is like a (really mixing metaphors here) recipe without any measurements. It says what elements should be in a Western Town, but does not say in what amounts.
This is the process of declaring what the variables will be called, and what sort of values they will contain. For now, just worry about "int"s and "String"s. "int" stands for integer, and "String" for a string of letters.
The semi-colon in Eunice's way of letting her editor know that she is done with the preceding statement and that he can move on to the next one. For the most part, you can think of it like a period.
Finally, you'll notice that whenever Eunice wants to group a set of statements, she uses a curly brace, "{". Even though Eunice knows her characters very well, this lets her editor see where a group of statements begins and ends. Any class will always end with all the open curly braces being closed.
If Eunice does create (instantiate) a town object eventually in her book, her editor will look to the same Western Towns class to determine what that town object should look like (Eunice will send the binders along with her manuscript). But where will her editor find out the number of stables and such? This is where we get into the constructor.
The constructor comes after all of the variables have been declared. It starts with public WesternTown(). The statements that follow are the default values for her variables; how Eunice's town object would look if she simply instantiated it in her plot without specifying any of its values.