9 Expedition into object interior

Rudolf Pecinovsky

Contents of whole series

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

Messages asking for an attribute value

In the last lesson you started talking about attributes and showed how I can get from an object a reference to its public attribute. However when I need this reference to pass it as a parameter, it still forces me to pass the object detouring through the object bench. Is there a possibility to pass the object directly as with strings?

You force me a little to write some code, but we can try it. You can send to an object a message asking directly for the value of its public attribute – in our case you ask the class NamedColor for the reference to its color. When you write the code for sending such a message directly into the text field for given parameter, the BlueJ sends this message to the object and passes the obtained value (reference) as the value of given parameter.

You write such a message in a similar way to an ordinary message: you address the object that you are sending the message to, after this address you write a dot and after the dot the name of the sent message, which is the name of the attribute. The only difference is the fact, that you may not add parameters and so you will not write any parenthesis. So if you ask the class NamedColor for value of its attribute BLACK, you simply write (pay attention to capitalization):


Figure 1: Message asking for reference to NamedColor BLACK

However I again want to emphasize that in this way you can ask only for values of attributes marked by the modifier public. The others you can only inspect.

Accessibility of attributes

You pointed to it already in the last lesson. However I then concentrated on how to get the reference and therefore I forgot to ask what the problem with the attribute publicity is?

The modifier public has nothing to do with publicity. It is the access modifier that means that it determines the access rights of other objects to the particular entity (class, method, attribute).

In the dialog box of inspection of class namedColor shown in the previous lesson there are several first attributes with the modifier private, the subsequent ones have the modifier public. The public attributes can be used by everybody, who can use the particular object (in our case the class NamedColor). In contrast the private attributes can be used only by their class and its instances and no one else can use them.

Does it mean that I can determine in the program, who can work with a particular attribute?

This subject needs a long explanation. For now I'll only reveal that the ability to encapsulate the processed data together with the operations working with these data and to hide the implementation details is taken as the most important feature of the object oriented programs. You can imagine it in such a way, that each object says to itself: “It's my private business how I do the things that I can do.”

As we said in the last lesson, objects save in their attributes the information needed for successfully fulfilling their functions. Programmers should think over very carefully which attributes can be accessible by other objects and which ones will be designated only for the private use of the object.

A vast majority of attributes are marked as private. However for some attributes the programmer decides that it can be useful to make them available also for other objects and he/she marks them as public.

However as I said: this subject is larger and more complicated, so we will return to it several times in future.

Well, I will remember, that I can freely work only with the public attributes. However how come that I can see the values of the remaining attributes? After all you have just said that these attributes are private and therefore secret.

The reason is that you are not an ordinary object. You are a developer and therefore the system accommodates you and it reveals to you even information which the other object has no chance to obtain.

The object inspector allows you e.g. to continuously watch the values of object attributes (including the private ones). It's very useful in the case, when the object's behavior is strange and you are searching for the reason.

Extending the test fixture

Oho, this starts to interest me. Show it in an example.

OK, but the existing test fixture is a little drab for this purpose, because all of its objects have the same positions and sizes. To diversify them, let's start by expanding it with a set of objects created by sending the parameter-less messages.

I think it will be strange when the old objects appear followed by the dialog window reporting that the fixture is ready and then the next objects are added.

Don't worry, we use the reversed technique. We use the fact that BlueJ remembers all our messages from the last reset of the virtual machine. We first ask the three shape classes to create an implicit version of our favorite objects. Only after that we ask the test class for the fixture containing objects created using parameterized constructors, whose preparation ends with the opening of the dialog box reporting that the test fixture is prepared.

Not to confuse our program we have to name the new objects with other identifiers to the objects in the actual test fixture. The object names in the test fixture end with number one (|rectangl1, ellipse1, triangle1). As the new objects will be created by parameter-less messages, we change the offered number one at the end of their identifiers for zero.

As I have said, after creation of the new trio we ask for the test fixture. Its objects will be added into the object bench and their creation will be reported by the dialog box. We confirm it and ask BlueJ to save the new version of the program creating the test fixture for the class Test. In the future we will work with this new version of test fixture. (You can see all the process in the animation).

Click here to run annimation

Animation 1: Adding a new set of shapes to the test fixture

Watching attribute values

I have the new test fixture created. We can start work.

Let BlueJ load the test fixture into the object bench, chose one object and in its context menu enter the command Inspect. BlueJ opens the inspector window.

Figure 2: Attributes of instance triangle1

Now try to change the object position and/or its dimension and watch in the dialog box, how the attribute values change.

You can even inspect several objects simultaneously and watch that after each action the value of the right attribute of the right object changes.

Static attributes – class attributes

What is the button Show static fields for?

As I said in the last lesson, the class has attributes of its own, too. It shares them with its instances. By pressing this button you open the dialog box inspecting the class attributes of the corresponding class.

In the last lesson we opened this dialog box by entering the command Inspect in the class context menu. Pressing the button Show static fields is only another way to open this window and have a look at the class attributes values.

Show an example where I can see that all instances share the class attribute.

When we have played with the triangles we can stay with them. Ask for inspection of class Triangle. One of its attributes is step. Its value determines the distance of movement forced by parameter#8209;less moving messages.

Figure 3: Attributes of class Triangle

You can set its value by sending the message

void setStep(int distance)

Set the value e.g. to 25 and send to both triangles from the test fixture a parameter#8209;less message asking for some movement. The distance of these moves is equal to the new value of the attribute step.

On the figure the value of the last attribute (count) is zero, but in my computer it is three. Why?

In the attribute count the class counts the number of created instances. There is zero on the figure, because I took this screenshot just after a reset of virtual machine when no instance was created. You have opened the window when the test fixture was created and therefore two instances of Triangle exist. Create further instances and you will see how the value of this attribute increases.

To be able to revise and try out everything, I again prepared a small accompanying animation that shows you everything we have been talking about. Run it and try to repeat all actions. Then you can try something similar alone.

Click here to run annimation

Animation 2: Attributes and working with them


Revise once more what we have learned today:

  • Object saves information about its state in attributes.
  • Classes as well as instances can have attributes of their own.
  • Class attributes are shared by the class and all its instances.
  • BlueJ allows inspect attributes of each object by the object inspector called from the context menu of the object by entering the command Inspect.
  • By sending a message with parameters we can write into the text field a code for sending message returning value instead of the required value. The returned value will be passed as the value of the appropriate parameter.
  • The attribute value accessibility to other objects can be set by accessibility modifier. The inspector dialog box shows the modifier of each attribute.
  • The values of attributers with modifier public are accessible for all objects that can access the “owner” of these attributes.
  • Attributes with modifier private serves only for the internal use of a particular object and no other object has access to them.
  • After processing a message all values shown in the inspector dialog box are brought up to date.

You can download the current version of the project here.

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