The Structure of a Java Source Code File

The java source code is in black text - the complete un-annotated version is at the bottom
explanations are in blue text


We usually start with a brief description of the pupose of the java file (written as comments).
// Press.java
the Java file name
// remember that the file name must match the class name exactly
// An applet for getting simple text input from a textField by pressing a button
// lines starting with // are comments (ignored by the compiler)
//
Next comes the import section, where we load the required, shared java libraries.
import java.applet.*; all the Applet Class definitions
import java.awt.*; all the GUI Class definitions
import java.awt.event.*; all the User-Action Class definitions
the import instruction the Java Class Libraries (the * means load All the Classes found in the specified library folder)


The first line of the class definition looks like this
public class Press extends Applet implements ActionListener
the visibility of the object
public or private
the type of object
a class
the name of the object
the class name Press
creates an instance of an Applet
to be viewed by a Browser
uses the ActionListener class
which listens for button clicks


{

this opening brace, and the matching closing brace at the end of the file, enclose the objects encapsulated in the class

TextField yourName = new TextField ("Type your name in here", 25);
the predefined AWT class type of the data member the variable name assigned to the data member instantiate the new data member ie. allocate the memory storage pass 2 parameters to the constructor method, the Prompt and the Field width

Button pressMe = new Button ("Press Me");
the predefined AWT class type of the data member the variable name assigned to the data member instantiate the new data member ie. allocate the memory storage pass a parameter to the constructor method, the Text on the button

Label theOutput = new Label ("This is where the output will appear");
the predefined AWT class type of the data member the variable name assigned to the data member instantiate the new data member ie. allocate the memory storage pass a parameter to the constructor method, the Text in the Label

public void init ( )
the visibility of the Method the return value of the Method the name assigned to the Method no parameters are passed


{

this opening brace, and the matching closing brace at the end of the Method, enclose the functionality of the Method

add (yourName);
the predefined Method to put a data member on the page the data member to be added

add (pressMe);
the predefined Method to put a data member on the page the data member to be added

add (theOutput);
the predefined Method to put a data member on the page the data member to be added

pressMe .addActionListener (this);
the data member we are listening to the predefined addActionListener Method to recieve action events listen to this button


}

this closing brace, and the matching opening brace at the start of the Method, enclose the functionality of the Method

public void actionPerformed (ActionEvent thisEvent)
the visibility of the Method the return value of the Method the name assigned to the Method the type of the parameter being passed the variable name assigned to the parameter


{

this opening brace, and the matching closing brace at the end of the Method, enclose the functionality of the Method

if (thisEvent .getSource == "pressMe")
the conditional keyword if the passed parameter the predefined Method to retreive the Button name that triggered the event the name of the Button being compared


{

this opening brace, and the matching closing brace at the end of the conditional block, enclose the functionality of the condition

String message = yourName .getText( )
the predefined LANG class type of the data member the variable name assigned to the data member the data member (textField) holding the text data the predefined Method to retreive the text from the textField

theOutput .setText ("Hello " + message + ", welcome.");
the data member (Label) recieving the text data the predefined Method to insert text into the Label the String message to insert into the Label Field


}

this closing brace, and the matching opening brace at the start of the conditional block, enclose the functionality of the condition


}

this closing brace, and the matching opening brace at the start of the Method, enclose the functionality of the Method


}

this closing brace, and the matching opening brace near the start of the file, enclose the objects encapsulated in the class



The Complete Java Code File looks like this

// Press.java
// remember that the file name must match the class name exactly
// Getting simple text input
// lines starting with // are comments (ignored by the compiler)
//
import java.applet.*;
import java.awt.*;
import java.awt.event.*; // classes used by the applet

public class Press extends Applet implements ActionListener
{ // Press class opening brace
    // a text field, a button and a label are initialised

    TextField yourName = new TextField("Type your name in here", 25);
    Button pressMe = new Button("Press Me");
    Label theOutput = new Label("This is where the output will appear");

    public void init()
    { // init Method opening brace
      // the objects are added to the page

      add( yourName );
      add( pressMe );
      add( theOutput );
      // Make the button "listen" for an event (eg a press)
      pressMe.addActionListener(this);
    } // init Method closing brace

    public void actionPerformed(ActionEvent thisEvent) // the button is pressed
    { // actionPerformed Method opening brace
      if (thisEvent.getSource == "pressMe") // which button was pressed eg. the 'pressMe' button
      {
        // Get the name from the text box, then give the label object the new text
        String message = yourName.getText();
        theOutput.setText("Hello " + message + ", welcome.");
      }
    } // actionPerformed Method closing brace
} // Press class closing brace


The Finished Java Applet looks like this

The applet before any user interaction After data has been entered and the button pressed


The original location of this page was http://www.uwcsea.edu.sg/comp/ib/javasource/SourceCode.html. It was mirrored from the web site of IB Computer Science, United World College of South East Asia, Singapore developed by IB Computer Science Teacher Richard Jones. This page was mirrored from that original site using the utility Webstripper. It was mirrored to ensure availability of it to local students as well as relieving hits on the original site. Aside from this paragraph and the immediately following footer, no other changes have been made.


[Counter On Strike [Home of Gerry Donaldson's Com Sci Gate]
[Gerry Donaldson's Email Address]
csgate@donaldson.org
ICQ# 62833374
[EFC Blue Ribbon - Free Speech Online]

URL:   http://donaldson.org/    Last Revised:   July 3, 2002