Java Tutorial - lesson two


Lesson Two: Your first object.

previous lesson - back to tutorial index - next lesson


This will be a rather long lesson. Do not be discouraged if you don´t understand everything in this lesson. You can experiment a little with the example below and get a slight feeling of Java. Understanding will come with experience.

In this lesson we will use the class of Lesson One with only one small change. We will add the line:
Calculator calculator= new Calculator();
The total code becomes:


 

//---------------------------------------------
// Class
//---------------------------------------------
public class Main {
//---------------------------------------------
// Methods
//---------------------------------------------
    public static void main(String args[]) {
        System.out.println("Starting the calculator...");
        Calculator calculator= new Calculator();
    }
}

 

 

And we will define a new class named Calculator. Save the next code as a separate file under the name Calculator.java in the same directory as the class Main.

 

import java.awt.*;
import java.awt.event.*;
//---------------------------------------------
// Class
//---------------------------------------------
public class Calculator extends Frame {
//---------------------------------------------
// Constructor
//---------------------------------------------
    public Calculator() {
        this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0); } });
        this.setBounds(100, 100, 300, 200);
        this.show();
        System.out.println("Constructing a new Calculator...");
    }
//---------------------------------------------
// Methods
//---------------------------------------------
}


Compile with ´javac *.java´. Both files will be compiled and both the classes Main and Calculator will be created. Execute your code again with ´java Main´.

Upon execution, you will start an empty and closable window. Notice the two text strings in your DOS/UNIX window.

Reviewing the code:

In Main, after displaying the string in your DOS/UNIX window, the new variable calculator is declared in the line ´Calculator calculator= new Calculator();´. This syntax means that the new variable calculator is of the type Calculator. A type that will have to be defined also.

The class Calculator is defined in the file bearing the same name with the extension .java. In a class, you can always find several elements: A class definition, one or more constructors (with the exact same name as the class) and a number of methods. With a constructor, you can define your variable. In Java this is called instantiating the class. Methods can be compared to procedures or subroutines in other programming languages.

The import statements allow the use of the Java classes Frame, WindowAdapter and WindowEvent in the code. Although it is not exactly the same, I tend to compare the import statements with the #include statements in C/C++.

´public class Calculator extends Frame´ defines the name of this class, and also indicates that this class is a kind of Frame. A Frame is a window that can be displayed on your desktop.

This class contains one constructor. As we have seen in main, a variable can be defined to be of the type of Calculator with this ´constructor´. In the constructor, we first make this window closeable with ´this.addWindowListener...´ but we will not go into the details.

Then, we set the position and size of this class, this window with setBounds. ´this.show()´ shows this class on your screen. The word ´this´ in java means ´this class´. We alter the size of this class, we show this class on the desktop...

Remarks: It is always possible to stop your java program by pressing ctrl-c in your DOS/UNIX window.

previous lesson - back to tutorial index - next lesson