M  

 

The Private Site of Fariborz Moradi


 
 

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, Laura 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 Laura'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 Laura wants to group a set of statements, she uses a curly brace, "{". Even though Laura 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 Laura 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 (Laura 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 Laura's town object would look if she simply instantiated it in her plot without specifying any of its values.

 


 



 

 
 
 
©2006 Fariborz Moradi       Contact