3 We send the first messages

Rudolf Pecinovsky

Contents of whole series

Note:
You can download the project we will use in this lesson here.

The project is compiled. So I repeat my question: “How should I send a message to somebody?”

In the context menu of every object BlueJ shows a list of all messages which the relevant object understands and which we can therefore send to it. The only objects we can now see are the classes. So we will start with sending messages to classes. Click the right mouse button on some class and look through its context menu. Figure 1 shows what you will see in the context menu of the class Rectangle.

Figure 1: Context menu of class Rectangle

The black commands in the top part of the menu represent messages that can be sent to this class. The red commands in the bottom part represent messages which can be sent to BlueJ. However we will pay no attention to them at this time.

Why is the “black list” divided into two parts?

The horizontal line separates the messages asking the class to create a new instance (we can say “the constructing messages”) from other messages. Notice that messages asking for a new instance are listed first (they are considered more important) and begins with word new.

Why are there so many messages asking for a new instance?

Because your demands for the properties of the object you want to create differ according to actual circumstances. If we remain with the rectangle whose context menu is opened in figure 1, then you can have either special requirements on the location, size and color of the created rectangle or otherwise you accept the default setting.

The class offers on the one hand the “full featured message” that allows setting of all customizable features of the created instance and on the other hand the “empty variant” without any demands on your setting for a situation with no special requirements or when you are satisfied with the implicit setting.

Creation of a new instance

So we will try to send the simplest one, the “empty variant” of the message asking for creating a new rectangle, or more precisely a new instance of class Rectangle.

It will be the best first step. We let the Rectangle create its new implicit instance. However there is one problem here: if you want to work with the created instance in future, you need the ability to address this instance and to be able to send it a message.

Fortunately BlueJ knows about this problem and therefore, even before sending the message, it asks you how it should name the created instance. It will offer you a name derived from the name of the class, whose instance you want to create.

Figure 2: Dialog box for creating instance without parameters

This time I recommend you to accept the offered name. Later I will explain what rules you have to keep if you want to create a name of your own.

I confirmed the suggested name and there appeared two rectangles: one in a new window and second below the class diagram.

Figure 3: First object created

The new window is a window of the canvas where you see the image of the created rectangle. The second, rounded rectangle is a representation of the program object for our future communication with the object. When you want to send a message to this object, you will find the message in the context menu of this representative, which we name reference to object.

Try to send messages to the created object and see how the object reacts to these messages. For now use only the messages without parameters beginning with the word new or void. (We will speak about the others next time.) You can see an example of such usage in an appending animation.

Click here to run annimation

Animation 1: Creating of the first instance

Virtual machine

At the end of the animation you spoke about resetting of a virtual machine. What is the virtual machine for and why do I have to restart it?

Virtual machine is a program that performs your program. The fact that you don't send your program directly to the processor, but to the virtual machine, has one important consequence: your program will not be reliant on one particular machine, but it can be run on any machine, where the virtual machine is installed. And nowadays it is almost impossible to find a computer, for which a Java virtual machine is not developed.

By resetting the virtual machine you cause it to forget everything that you have done before and you can start everything again from the beginning. You need not therefore be afraid of your experiments, that it will retain some garbage from a previous (or to a next) session.

And when I don't want to forget everything?

Everything, which shouldn't be forgotten, you can save on the disk. For the next session you can take it and continue in your work.

Revision

Revise once more what we have came to know today:

  • In the interactive mode we send messages to objects by entering appropriate command from their context menu.
  • The black items in the upper part of context menu represent messages to appropriate object; the red items in the bottom part represent messages to the BlueJ.
  • The “black items” are also divided into two parts: the upper one contains the messages asking for creation of a new instance, the bottom one contains the remaining messages.
  • Objects often offer groups of messages containing variations of one message. Particular messages in such a group differ in requested parameters.
  • When you ask a class in BlueJ for a new instance. BlueJ first asks you, what name the proxy representing the created object should have – we name it reference to the object.
  • After creating an instance BlueJ adds a new shape into the object bench. This shape will represent a corresponding object. Its context menu will contain items for all messages, which can be sent to the represented object.
  • There are two text lines on this shape: the first line contains the name of reference to the object, the second line contains name of its class.
  • The virtual machine is a program, which performs our programs.
  • Resetting of the virtual machine causes forgetting of all unsaved information.

Contents of whole series

Rudolf Pecinovsky
(rudolf.pecinovsky@i.cz) is a senior EDU expert in ICZ, Inc. and associate professor of software engineering at The University of Economics, Prague. He has more than 20 years experience in programming education. Rudolf published 39 books in five national languages. His latest book on design patterns was launched in September 2007.

Project Features

Project Links

About this Project

edu was started in November 2009, is owned by Antonin Nebuzelsky, and has 86 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close