5 Messages asking for values

Rudolf Pecinovsky

Contents of whole series

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

Sending simple messages I have enjoyed enough. Previously you promised that you would tell me about messages which begin neither with new, nor with void. I think that promised next time just happened.

You are right, let's do it. The messages we have sent to created instances till now, ask these instances for some (visible) action. In our programs we often don't need the addressed instance to make something, however we need it to disclose something (mostly about itself).

To be able to work with the obtained information effectively, we need to know, what it is for. With numbers we will work in a different way than e.g. with logical values and with logical values we will work with a different way than e.g. with texts.

In context menus of classes and their instances every command for sending messages starts with the name of the type of the returned value.

What returned value? How it can return anything when we lent it nothing? We only sent a message to it.

Take it as a programmers' term. You send a message to object and the object can return a value.

Well, I will cope with it somehow. Continue in your explanation.

Till now we have worked with two types of messages:

  • The messages, which start in a context menu with the word new. Such messages can be sent only to a class. In reaction to this message the class creates a new instance of its own and returns reference to it.
  • The messages, which start in a context menu with the word void. Such messages ask for some action after which the addressed object returns nothing.

Now we add a third type of message: messages asking for some value. In the BlueJ context menus the text of commands sending these messages starts with the type of returned value.

Type of returned value

Demonstrate it with an example.

Well. The virtual machine is reset, so we can start again from the beginning. Have a look at the context menu of class Triangle. In the second set you can see the message

int getStep()

Figure 1: Command sending a message getStep()

The word int at the beginning of the command is an abbreviation of integer and tells us, that after receiving this message the addressed object returns an integer. I disclose to you, that this number is the number of picture elements (in abbreviation pixels), which the instance moves, when we ask it to move and don't say, how far.

Getting the returned value

And how do I know, what the addressed object has answered?

When you send such a message, BlueJ passes it to the addressed object, receives a return value and opens a dialog box where it discloses, what the addressed object answered.

Figure 2: Dialog box with the message return value

Now I should make sure that it isn't bullshitting us.

I have prepared a small animation that will demonstrate everything. It first asks the class Rectangle for the default size of steps. Then it asks for its instance and asks the instance for its x coordinate. It follows by sending a message to move right and then asks again for the x coordinate of moved instance. Run it and try to repeat its actions. You can naturally try other movements, too.

Click here to run annimation

Animation 1: Sending messages requesting returned value

Primitive and object types

It was simple. However, near the top of the instance context menu I have seen the command NamedColor getColor(). So I have tried it and I have not obtained any color - instead of a returned value some arrow appeared.

With colors it is a little complicated because unlike numbers named colors are objects.

Why “unlike numbers”? When you have told me about the principles of OOP you said, that in OOP the object is everything that I name with a noun. Why then is the number not an object?

In pure OOP it is really so, as I have said. However, the authors of Java wanted the programs to run as quickly as possible. Therefore they divided the data types into two groups:

  • The eight data types, which are directly supported in the instruction set of most current microprocessors, are classified as primitive data types. The virtual machine converts the operations with data of these types directly into instructions for the processor in use.
  • All the other data types (in the standard library there are more than 18,000 of them) are classified as object data types. We work with them in a significantly different way.

Which data types are the primitive ones?

There are four different forms of integer numbers, two forms of real numbers, characters and logical values. Down the road I will give you more detailed information about each of these types. For now we will restrict ourselves to the integer numbers of type int, which we have already met.

Well, let's suppose that I agree. And what about object types?

As I said, with objects we work in a significantly different way. All objects are created in a special part of memory named heap. Whenever you want an object to make something that means whenever you want to send a message to it, you should send it through the reference to the object. On the other hand whenever you ask for an object, you get only a reference to it instead of the object itself.

Good heavens, why is it so complicated?

Maybe it seems a little strange to you, but the main motive is the productivity of the development and the reliability of the resulting programs. During the course of the program a lot of objects are continually created and destroyed. Something has to allocate memory for the new objects and clean up after the dead ones. This activity is the task of a special part of the virtual machine – the garbage collector.

Experience shows that if the garbage collector, whose function is definitely crucial, is to work reliably we mustn't allow anybody to touch it. Therefore programs can speak to objects only through moderators – their references.

Before I started to program in Java I had been programming for more than 15 years in C++. In the first few weeks of Java programming I had a permanent grumble that this or that can be made better and more effectively in C++. After two weeks I discovered, that although I grumbled all the time, I worked more than twice as quickly as I was accustomed with C++. Java didn't allow me to make many of the mistakes which are very common in C++ and which makes the whole development slower. Then I started to like Java.

References to objects

I don't know how I can increase the productivity by making something more complicated.

However in reality it is not at all complicated. It is similar to TV. In past you had to go to TV and press the corresponding button or turn the corresponding knob every time. Later the producers made it complicated: they added a remote control. Most people I know take this “complication” as a quite useful enhancement.

Take the reference to an object as the remote control of the object that is unreachable by other means. This concept is useful even for explanation of other features of the references.

  • The moment you loose or destroy the remote control (= reference) you also loose any possibility of determining the future behavior of the controlled object.
  • You can control one TV by several controllers simultaneously. The TV reacts independently of the controller that sends the command. It is similar with objects. You can have several references to one object. This object will react independently of the reference which passes it the message.

I think we have spoken about it enough and you should show me how to work with objects.

It's easy. Ask the class Test for a fixture. Then ask any of the created objects for its color. BlueJ opens a dialog box where it tells you that the object responding to the message asking for an object (=color) returned a reference to the object you asked for. The shown arrow symbolizes the reference nature of the returned value. BlueJ allows you to add this reference into the object bench.

Figure 3: Dialog box with a reference to object

To add the reference into the object bench you should press the button Get. BlueJ asks you, how it should name the added reference, and then it adds it there. You can use this reference in the same way as the references obtained by creating new instances.

I see that often something asks me for a name. Would you finally explain to me how to construct a correct name?

I think it wouldn't optimal to overload you with further information. Let's therefore postpone the explanation of the rules for creating names (or professionally called identifiers) to the next lesson.


Revise once more what we have learned today:

  • In the object's (including classes) context menus we can recognize three sorts of messages:
  • The commands in the class context menu starting with the keyword new send messages asking the class to create a new instance.
  • The commands starting with keyword void send messages asking for some action after which the addressed object doesn't return anything.
  • The remaining commands start with the name of the type of the value returned by addressed object as a response to corresponding message.
  • Java sorts the data types into two categories:
    • The eight basic data types, which are directly supported in an instruction set of most current microprocessors we call primitive data types. Their operations are converted directly into the instruction set of the microprocessor in use.
    • The remaining data types are called object data types. With their values, which means with particular objects, we communicate through reference to them.
  • The returned values of primitive types are shown directly in the dialog box opened after finishing the requested action.
  • The addressed object that receives our message doesn't return directly the asked object but only references to it. BlueJ enables us to name this reference and save it into the object bench.
  • The described technique doesn't allow us to make the most frequent errors and in comparison with older languages (e.g. C++) it roughly doubles the productivity.
  • We can compare the work with references to controlling the TV set by the remote controller.
  • 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 87 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
    Please Confirm