A SIMPLE JAVA PROGRAM
An important driving force for the adoption of OO languages has been the rise of GUIs (graphic user interfaces) starting in the 1980s. To write the C code to handle even the simplest window is unbelievably complicated. Just as the use of subprograms allowed early C programmers to start with a library of reliable functions, using the correct OO language lets you start with some complicated capabilities, all reliably debugged and documented.
In Java, you write your entire program as a logical unit called a class . All of the code for a class is contained in a single text file and is compiled by the Java compiler into a single file of computer instructions. To create a running program, the Java runtime environment creates one or more objects using the compiled code as a template.
Here's the complete Hello World program in Java.
import java.awt.*;
public class MyFrame extends Frame {
public static void main(String[] args){
MyFrame mf = new MyFrame("MyFrame");
mf.show();
}
public MyFrame(String title ){
super( title );
setSize( 200, 120 );
add( new Label("Hello World", Label.CENTER ));
}
}
Similar to the other C family languages, the code starts with an
import line that tells the computer you are going to be using a
library called java.awt . The final * says you may
use any class in that library. AWT stands for Abstract Windowing Toolkit --
or, if you're having a hard time with it, Annoying Windowing Toolkit.
Next you get to the most important line -- the class declaration . This says that
your program is going to be in a class named MyFrame and that
it's going to extend a class named
Frame . The Frame class comes from the
java.awt library and is the typical starting point for Java
programs that need a GUI window.
Because MyFrame extends Frame , your class is able
to use all of the cool capabilities that Frame provides, just
like that. Here's a screen shot of a running MyFrame program on Windows.
You can drag that window around the screen, resize it, or minimize it because
MyFrame inherits all of those capabilities from
Frame .
Okay, back to the code. Look at the next chunk of code in MyFrame, the
main function.
public static void main(String[] args){
MyFrame mf = new MyFrame("MyFrame");
mf.show();
}
Similar to C, the convention is that programs start by executing a function
named main . If the Java runtime can't find a main
function, it can't run a program. The other parts of that main
function declaration are glossed over here to avoid getting bogged down in
detail. You come back to this in later lessons.
The second line creates a variable of the MyFrame type that's
named mf and says to create a new MyFrame object to
store in mf . The keyword new that appears in this
line is unique to OO languages. It says to use the MyFrame class
as a template to create a new object. At this point the object has been
created, but not yet shown onscreen.
The line mf.show() ; says to call the show function
that belongs to the MyFrame class. You don't see a
show function defined in the MyFrame class because
you inherit that function from Frame , which in fact inherits
from another class called Window . Your MyFrame
class can use functions and variables from a complete hierarchy of classes, all the way back to the most
primitive Java class called Object .
The MyFrame Constructor
The remaining code in MyFrame is a constructor . It has one parameter, the string representing the title that you want the graphic window to display:
public MyFrame(String title ){
super( title );
setSize( 200, 120 );
add( new Label("Hello World", Label.CENTER ));
}
Although it looks similar to a function declaration, a constructor is in a
special category. Note that it doesn't declare a returned variable.
Constructors always get invoked by the new keyword and carry out
a special set of instructions. All of the classes from which the
MyFrame inherits, all the way back up the hierarchy to
Object , have to be called to build a MyFrame
object.
This is accomplished by the super( title ) ; line that calls a
constructor in the Frame class. In particular, it calls a
version of the Frame constructor that takes a title string
parameter. After that, the line setSize( 200, 120 ); calls a
function (inherited from Frame ) to set the size in pixels of
the space MyFrame takes up onscreen.
You don't inherit any method from Frame that shows a message
because a Frame is intended to hold other GUI components that do
handle messages. To get the words "Hello World" displayed, you create a
Label object and then add it to the Frame . The
Label class is part of the java.awt library you
imported. Using the new keyword, you call the Label
constructor with a message and a special constant that tells the new
Label to show the message centered in the area it occupies in
the Frame .
That's all it takes to create a custom version of a GUI window display. Of
course, it doesn't do anything but show up on the desktop display, but you
can add functionality by defining more functions in the MyFrame
code.
