Section 9e

From the Beginning - Creating a Class


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Keeping It Simple

There is not much to creating a class in Java. First, you must create the class in your editor, as follows:

public class BoxClass
{

}

Then, for all public classes, you must save it in a file with exactly the same name as the class and the .java extension on the file name: BoxClass.java. That's it. Class created. Of course there is the chewy, nougaty center to fill out but that will come.

In most cases, you must create a package name which will be the folder in which the class resides, and that will be placed above the class name, as shown here:

package boxclass_package

public class BoxClass
{

}

For purposes of structured programming, there should be an organization to your class components and for purposes of this online reference, it will look like the following:

package boxclass_package

public class BoxClass
{
< all class constants here >

< all class member data here >

< all constructors here, 1) default, 2) initialization, 3) copy >

< all member methods here, in alphabetical order >
}

This organization is not a law but a convention, meaning that all the folks studying this online reference will do it this way but others may not follow the exact same format. That said, the folks that don't follow this convention should have some standard format for their classes, or the concept of structured programming will be lost.

Note, as mentioned previously, there is no place to put the main method. That is because real abstract data type classes, meaning almost all of them, will not have a main method in them. As also mentioned previously, the main method is a driver that will use this class but it will be in its own class. That will be discussed soon.

Also note that the three constructors offered are always a good idea. However, there are some circumstances where fewer constructors are needed or appropriate. As an example, the Console_IO_Class only has one (default) constructor and since the class does not really store any data, the constructor is empty. While it could have been left out, Java will always create a default constructor if the programmer does not, so it is a good practice to always have at least one constructor.

As you may have guessed at this point the class that will be used to demonstrate these operations will be called BoxClass. Remembering that classes are not actually programs and don't really solve anything, your class will be constructed in the form above, and in most cases, you will know in advance which member data and methods will be required in the class. With that in mind, let's take a look at the class constant component. Parts of the above code will be shown so you can have a reference with which to work. The new parts will be shown in red.

public class BoxClass
{
// constant unit size default
private final int UNIT_SIDE_SIZE = 1;

// constant default color
private final String DEFAULT_BOX_COLOR = "Black";


*
*
*

The constants tend to be private although there are reasons they might be public. That will be a judgement call as you develop your classes. When we derive this class to an inherited class, we will actually change these keywords to protected, but that will be in the next section and will be discussed there. Note that the keyword to indicate constant in Java is final and note that you must show the data type of each constant value and you must actually set the value at this location.

Next up is the member variable component, shown here

*
*
*

private final String DEFAULT_BOX_COLOR = "Black";

// private box length and width
private int length, width;

// private box color
private String color;


*
*
*

In many cases, the member variables will be separately declared. However, for this circumstance, the length and width of the box are both integers and are both simple member variables representing a box, so in this case, it is not so bad to declare them together. At this point, we have a box that has length, width, and color. Important note: Unlike the constants, you do not assign member variables in this area; they will be assigned in the constructors.

Let's take a look at the default constructor.

*
*
*

// private box color
private String color;

/*
name: constructor - default
process: initializes object to default values provided as constants
method input/parameters: none
method output/parameters: none
method output/returned: none
device input/keyboard: none
device output/monitor: none
dependencies: none
*/
public BoxClass()
{
// set length
length = UNIT_SIDE_SIZE;

// set width
width = UNIT_SIDE_SIZE;
// set color
color = DEFAULT_BOX_COLOR;
}


*
*
*

Now this has a little more to talk about. The specifications you remember from the Six Step Programming Process are there but they are pretty simple. All constructors in a given class are used to initialize the object that is created from the class; that is, they set it up and get it ready to do its job as a specific data quantity. An important consideration related to constructors is that they are responsible for initializing all member data in the class. In the case of the BoxClass, the length, width, and color must be set to some values before the object can be used so in what is called a default constructor, default, or simple starting values, are used to get it initialized.

Notice that constructors always have the same name as the class and they do not return anything. Let's consider another one.

*
*
*

color = DEFAULT_BOX_COLOR;
}

/*
name: constructor - initialization
process: initializes object to values provided as parameters
method input/parameters: length & width (int), color (String)
method output/parameters: none
method output/returned: none
device input/keyboard: none
device output/monitor: none
dependencies: none
*/
public BoxClass( int setLength, int setWidth, String setColor )
{
// set length
length = setLength;

// set width
width = setWidth;

// set color
color = setColor;
}

*
*
*

The method also has the same name as the class. This means there are two, and below, three, methods with the same name in this class. In most OOP languages this is acceptable and is called method overloading, or just overloading. No harm, no foul. In this constructor, called an initialization constructor, the user/programmer provides data to the constructor to get it started with the data the programmer wants to use. Note again that the constructor must make sure all the member data within the class is initialized. This is critical since uninitialized member data can cause all kinds of problems as the class is developed and later used. It is faster than using a default constructor and then individually setting the member quantities. Take a look at one more kind of constructor, the copy constructor:

*
*
*

color = setColor;
}

/*
name: constructor - copy
process: initializes object to values provided from another object
method input/parameters: copied object reference (BoxClass)
method output/parameters: none
method output/returned: none
device input/keyboard: none
device output/monitor: none
dependencies: none
*/
public BoxClass( BoxClass copied )
{
// set length
this.length = copied.length;

// set width
this.width = copied.width;

// set color
this.color = copied.color;
}

*
*
*

Now this method might make some folks uncomfortable, for a couple of reasons. If you will notice, the data from the other object is being copied straight across without any accessors. It is as if this object can reach right into another object and grab its private data . . . , and that is exactly what is happening here. Since all objects are created from the same class, it is assumed that one object can access the private data of another, at least here in a constructor. It is also possible for this action to happen in other methods but it is not as common. You will rarely, if ever, use accessors such as getWidth or others in constructors; it is just not necessary.

The second possible point of discomfort here is related to the this reference. There are circumstances where the data between two objects is being shared or manipulated, and in these circumstances, it is possible to confuse whether the variable is related to this (local) object or the other (incoming) object. For clarity, Java invented the this reference. The truth of the matter is, as long as your code uses self-documenting variables and is clearly written, use of the this reference is not really required in most code. That said, it is available to those folks who think it might help in the clarity of the code, so feel free to use it. There are potentially some circumstances in more advanced programming that could call for using this but it's not otherwise very common.

From here forward, there are another eight methods. However, we will just consider three of them and leave the rest for the video presentation. The first method is a processing method, shown here:

*
*
*

/*
name: find2DArea
process: calculates two dimensional area of the box
method input/parameters: none
method output/parameters: none
method output/returned: calculated area (int)
device input/keyboard: none
device output/monitor: none
dependencies: none
*/
public int find2DArea()
{
// calculate and return area
return length * width;
}

*
*
*

First, note that this method has a return type (int). All methods that are not constructors must show a return type even if it is void (nothing returned). Next, note that the method does not accept any parameters. This is not necessary because the object already holds all the data pertinent to the box; remember that this is both encapsulated (i.e., part of the package that is a BoxClass) and hidden data (i.e., data that is not directly accessable and protected from inappropriate modification by the class). Finally, note the simple operation of just returning the product of the length and the width. This is perfectly acceptable as long as the code does not get too complicated and/or difficult to read. Otherwise, this method is pretty straightforward. Its name is find2DArea, and it calculates and returns the box' two dimensional area. Most methods like this will start with the verb calc or find but there are other options in some cases.

Next, let's take a look at an accessor, known colloquially as a "getter":

*
*
*

/*
name: getColor
process: returns box color
method input/parameters: none
method output/parameters: none
method output/returned: box color (String)
device input/keyboard: none
device output/monitor: none
dependencies: none
*/
public String getColor()
{
// return box color
return color;
}

*
*
*

Like many methods written in classes, this is another pretty simple one. Because the data is hidden or protected, the user/programmer is not allowed to just get the data from the class without asking (nicely). On the other hand, handing it over is just as easy as returning the member data. Note for this one, the return type is String because the box color is stored as a String value.

Finally, we will take a look at a mutator or "setter" method, but it is not much more complicated. To wit:

*
*
*

/*
name: setLength
process: sets box length to new length
method input/parameters: new box length (int)
method output/parameters: none
method output/returned: none
device input/keyboard: none
device output/monitor: none
dependencies: none
*/
public void setLength( int newLength )
{
// set length to parameter
length = newLength;
}

*
*
*

As mentioned, simple. Note that in this method, the return type is void since it doesn't return anything. It just sets the protected data quantity to the newLength value. Other than that, and as you will see, it's just another method. They never really have to be difficult although they can get interesting when the method is used to solve more algorithmic issues. That will come but it won't be happening any too soon.

This section focused on the creation of a class from the ground up, and with lots of supporting description. You should watch the video and work along side it to write your own class and get comfortable with this process. The video also incorporates some of your step 3, 5, and 6 into the actions. As you progress through this online reference, you will be writing a lot of methods; the practice is worth it.