8 Object parameters

Rudolf Pecinovsky

Contents of whole series

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

Message declaration and sending

I have thought about the last lesson and I want to ask, why the caption under figure 3 is
We send the message IO.inform("Fixture prepared") to class IO
when in the dialog box, where we are setting the parameters' values, and in the context menu this message is given in form
void inform(Object text) ?

The descriptions in context menus give you hints what the meanings and types of particular parameters are and what type the return value will be. To the contrary the caption shows the form used in a program. There you don't write the declaration of names and types of particular parameters, but their values.

Look at the mentioned dialog box better and you will see both forms of the message there:

  • over the dividing line the context menu form is used,
  • around the input text field, where you put the parameter value, it is written in the second form that is used in the caption.

You explained that the form used in a context menu begins with the name of the type of the return value. Why does the second form start with IO?

As I have said, you write messages in a program in this way. Each time you have to write the recipient first. After it you write a dot and after the dot the message to be sent. When sending messages we put the values instead of declarations of their types and names.

Because we want to send the message to the class IO, BlueJ wrote in the dialog box first the name of this class and I made the caption the same.

If I understand correctly, the first form should indicate the purpose of the message and how to use it and the second form is the written form of the actual sent message.


Passing a returned reference

By mentioning the written program you reminded me of the original purpose of our session. I have to say I would like to change the present playing with objects for real programming.

In the next lesson we launch into this field. However now I would like to show you how to send messages with object parameters.

You are right. You promised it in the last lesson and you haven't shown it yet.

I am hurrying to rectify that. Reset the virtual machine and ask the class Test for our favorite fixture. Then ask the rectangle for its color and save the obtained reference into the object bench with the name RECT.

Now enter in the context menu of the triangle the command

void setColor(NamedColor color)

BlueJ opens a dialog box asking for a reference to the color you want to set.

There are several ways of entering the reference. The simplest one is to enter its name. In addition if you have this reference prepared in the object bench, you can click on it and BlueJ writes its name for you.

Try it. Click in the object bench on the reference to the rectangle color (we named it RECT) and BlueJ writes its name into the input field. Now confirm the entry and the triangle gets its color set to the red color of the rectangle.

It really has got another color. But what if I want to paint it with some color that isn't the color of an existing object?

You can ask the class NamedColor for it. Have a look at its context menu. In the list of messages you can see the message

NamedColor getColor(String name)

You enter the name of the required color in the parameter field and the class returns the reference to this color. You enter this reference name to color parameter field of the dialog box of the message setColor(NamedColor) and shape's color changes.

However, how do I discover what colors the class knows?

You ask it. Send it the message

void showKnownNames()

The class then opens a dialog window, where all defined color names will be written. However take into account that a color can have several names (e.g. ocher is called also ochre) and therefore there are less colors than color names.

Figure 1: Known color names

Run the accompanying animation and let it show you everything step by step. Then you can try something similar alone.

Click here to run annimation

Animation 1: Messages with object type parameters

Class and instance attributes

Everything works exactly as you described, however I cannot believe that there is no simpler way. Can I ask for an instance in a direct way?

You force me to dive into objects. Till now we have shown how the instances react to messages we send them. We still haven't said anything about how the instance recognizes, where it should paint itself, where it should move from or how it recognizes its color.

The object has to keep information describing its state to be able to react to the received messages. It keeps this information in its attributes.

Some authors don't use the term attribute, but the term variable or field. I prefer the term attribute, because it is a term used in general OOP and in addition it distinguishes the just discussed meaning from other meanings of the terms variable and field.

Each class defines which attributes their instances will have (all instances of the same class have the same set of attributes) and which attributes the class itself will have. Each instance has the set of instance attributes of its own and it takes care of its set.

On the contrary the class attributes are common to a given class and to all its instances. In other words all the instances share the class attributes of their class.

You can imagine it in such a way, that you have a family (class) and its members (instances). All family members will share one family bank account (class attribute), but each of them has a wallet of their own (instance attribute) with the actual cash. Once somebody changes the account state, it immediately influences all the family members. However the cash in the wallet is a private business of each of them and its change has no influence on the others (except in the case, when this person fills their wallet from the common account).

Working with attributes

I hope I understand the concept of attributes. Now tell what I can do with them.

BlueJ offers the possibility to inspect each object and ascertain the values of its attributes. In addition you can get reference to public attributes of object types and let BlueJ save this reference into the object bench.

However how do I recognize that a given attribute is public?

Don't worry, it is written together with its name and type in the dialog box opened after your demand to inspect the object.

Aha! Now it starts to be interesting. So show me how to do it.

It's simple. A few minutes ago you asked for a simpler way of getting an instance of class NamedColor. Let's have a look what this class offers to us. Open its context menu and enter the command Inspect. BlueJ opens a dialog box that you can see in figure 2.

Figure 2: Inspection of class NamedColor

The dialog box shows a list of attributes that is so large that you need a scrollbar for their overview. The first several attributes are not interesting to us for now, because they are marked as private. However below them starts a group of public attributes representing particular colors.

When you click on one of them (let's agree, that you click on attribute BLACK), the buttons Inspect and Get activate. Pressing the button Inspect opens another dialog box allowing looking inside the selected instance. Postpone trying out this possibility for later when you know more and for now press the button Get. BlueJ opens a dialog box and asks for a name for the new object and saves the reference to it into the object bench.


Revise once more what we have learned today:

  • When we declare the message we write the type of the return value followed by the name of the message. When we send this message, we write first its addressee followed by a comma and name of sent message.
  • When we declare the message, we write in the brackets after its name the types and names of parameters. When we send it, we type instead of them the passed values of the parameters.
  • We can set the values of object parameters by entering the name of the reference to the corresponding object.
  • If we click on the reference in the object bench, BlueJ writes its name into the actual input text field.
  • Objects save information about their states in attributes.
  • Some authors don't use term attribute, but term variable or field.
  • All instances of given class have the same set of attributes.
  • A class (like instances) can have a set of attributes, too.
  • Class attributes are shared by the class and all its instances.
  • BlueJ allows inspection of attributes of each object by the object inspector called from the context menu of the object by entering the command Inspect.
  • The inspector window offers to get the value (reference to object) of public attributes of object types.

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