Section 9c

Static Operations vs Standard Classes


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Kinda Like OOP vs The Real Thing

Static vs Standard

Up to this point, almost all the classes you have used have been static, meaning the class is not actually instantiated and it incorporates the main method. The one exception has been the Console_IO_Class which is more like a standard class. However, the Console_IO_Class is really a utilities class so it is not really the abstract data type (ADT) that has been discussed in the previous sections, although it still qualifies as a non-static or standard class.

As a note, classes using static components are not necessarily static classes; that's another story for later. Nevertheless, classes that use static components do not have constructors; they don't need them. Constructors will be addressed in the next section but for the time being, be aware that this is a significant difference. While standard classes (like the Console_IO_Class) are instantiated using constructors, classes that use static components are simply called as a class and the main method goes to work. A class using static components and the method within it is much more procedural than it is object oriented. That said, no matter how many classes are created to support a program, the program itself still has to use them to solve a problem or create a solution, so there will always be a main method involved with every running program. Consider the following program, and note the Console_IO_Class in red:

public static void main( String[] args )
{
// initialize program

// initialize variables
Console_IO_Class mainIO = new Console_IO_Class();
double sideOne, sideTwo, hypotenuse;

// show title
// method: showTitle
showTitle();

// get triangle sides from user

// get triangle side A
// method: getTriangleSide
sideOne = getTriangleSide( "Enter Side A: " );

// get triangle side B
// method: getTriangleSide
sideTwo = getTriangleSide( "Enter Side B: " );

// calculate hypotenuse
// method: calcHypotenuse
hypotenuse = calcHypotenuse( sideOne, sideTwo );

// display results, including input

// show input values and hypotenuse
// method: showResults
showResults( sideOne, sideTwo, hypotenuse );

// end program

// show program end
// method: printString, printEndline
mainIO.printString( "End Program" );
mainIO.printEndline();
}

This is a slightly updated version of a program found in one of the previous videos. Unlike a class, this is a program that actually solves a problem. Due to the readability, you should be able to see exactly how it works and what it does. Note that the Console_IO_Class, in red, is instantiated in the main method with the object reference mainIO. The Console_IO_Class is NOT a static component-using class and is more like a standard class in that it must be instantiated using the new operator and a Console_IO_Class constructor. This is another example to distinguish the difference between static vs standard classes.

It should be clear that this main method (i.e., the program) needs some supporting methods so they should be considered, The headers for these methods are shown here:

public static double calcHypotenuse( double sideA, double sideB )

public static double getTriangleSide( String prompt )

public static void showResults( double side_1,
double side_2, double hypotenuse )

public static void showTitle()

Notice that every method must have a static keyword in front of it to play correctly with all the other static methods. To repeat, this is a class using static tools as opposed to what we will call a standard class. Finally, take a look at the constants declared at the class level above the main method:

private static final int TWO_END_LINES = 2;
private static final int PRECISION = 2;

Again, they have to have the static keyword. You can also note the public and private keywords but they will be discussed in the next section.

The difference between static and standard classes is not very complicated and will become clearer as you develop your own real ADT (and other) classes. However, it is important that you can distinguish between them as you work with Java code.


Watch this video to compare static oriented programming with object oriented programming