# Writing Code: The Class and Class-Side Methods

> Now that the setup work is complete, we are now ready to begin writing actual code! The first thing we need to do is create an application class. We will be subclassing off of Application. To do this, open a class browser, and select the "Application" class in the left-hand pane. Once you've selected this class, right-click on it, select "Make subclass...", and type `ManHourCalculator` in the resulting box. If you did this correctly, you should see that the tree for Application has expanded, and our new "ManHourCalculator" class is under it. The browser should look like this:

> If your class appears anywhere else in the tree, you did not have Application selected when you created a subclass. You can correct this by clicking on your "ManHourCalculator" class, clicking on the "Class" tab. Then change the first word from whatever it is (probably Object) to Application, and accept the change.

> Before we start defining methods, we need a few instance variables to hold the values of our widgets. (In a "real" application you would most likely have an entire class that represents your data model. However, to simplify this application we will be using instance variables within our main application. This will probably give some true OO people heartburn, but I really wanted to make certain that the details didn't get too complicated.) Click on the "Class" tab in the editor pane; you should then see the definition of our class. To make it easier for you to declare the instance variables, I've just copied the entire class definition below:

>     Application subclass: #ManHourCalculator
>         instanceVariableNames: 'popupListModel hourFieldModel adjustedFieldModel'
>         classVariableNames: ''

> These three variables will be holding objects that hold values for our widgets. We will be creating those in a minute.

> The first method we will define is the class method `formID`. (From now on I will use standard Smalltalk shorthand for methods: class methods will look like this `ManHourCalculator class>>formID`, and instance methods will look like this `ManHourCalculator>>initialize`.) To create a class method, click on the "Class" tab of the right-hand pane, right-click inside it, and select "New method." It would appear as if nothing has happened, but the editor pane is now ready for you to enter a complete method definition. Type and accept the following code exactly as shown:

>     formID
>         "Answer the form to load for this class."
>         ^##MainForm.

> This creates a class method called `formID` that returns the constant for the main form of our application. If you typed this correctly, then pressing `Ctrl-S` should cause a method name, formID, to show up in the right-hand pane. That is the only class side method that we will be defining.

> We will now override a class-side method on the `Smalltalk` class. The method in question is `Smalltalk class>>start`, and it is what will cause our application to startup when you click on it on your Palm. To do this, find the class `Smalltalk` in the left-hand pane of the class browser, select it, then click on the "Class" tab in the right-hand side. Click on the plus sign next to "startup", and then click on "start." This is what you should see:

> You want to replace "Put your startup code here!" with this:

>         ManHourCalculator show.

> Accept your changes. There is one more step with this method that must be taken. If we were to leave things as they are now, then when you exit PST, and restart, your redefinition of `Smalltalk class>>start` would be gone. This is because we told the environment not to save changes to the core package, and we didn't say where we wanted our new version to live. That's what we need to do now. To do this, right-click on the `start` method in the right-hand pane, and select "Change package...". Your "ManHourCalculator.st" package should already be selected in the resulting dialog box, so click on "OK." Now when you come back into PST, this change will still be there. We have to do something similar for an instance method on the "Application" class, but we will save this for later.

> << Section 3: Defining The Constants | Section 5: Writing Code: Instance Methods >>
> Introduction

Copyright © 2000 Joey Gibson