6 Messages asking for objects

Rudolf Pecinovsky

Contents of whole series

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

Rules for creating identifiers

In the last lesson you promised, that today you would teach me how to construct a correct name.

You are right. And promises should be kept, shouldn't they?

First I reveal that we use the term identifiers for names, because they allow us to identify particular parts of the program. The rules for creating identifiers are simple. There are four. Identifiers:

  • can contain only letters, digits and two other characters: _ (underscore) and $ (dollar); however the usage of $ is not recommended;
  • may not start with a digit,
  • are case sensitive (hello, Hello and HELLO are three different identifiers),
  • may not have the same spelling as any of the following 50 words – the keywords:

abstract      continue       for            new            switch
assert        default        if             package        synchronized
boolean       do             goto           private        this
break         double         implements     protected      throw
byte          else           import         public         throws
case          enum           instanceof     return         transient
catch         extends        int            short          try
char          final          interface      static         void
class         finally        long           strictfp       volatile
const         float          native         super          while

What are the keywords?

The keywords are words, which have a special meaning defined in the particular programming language. We have met some already (the keywords int, new and void), the meaning of the others we will gradually reveal in following lessons.

Getting a reference to a returned object

Now, when I know, how to define names or (if I speak professionally) identifiers, I could immediately try it.

Why not? Ask for the color e.g. the rectangle and because you know that its color is red, give this name also to the created reference. BlueJ then adds this reference into the object bench.

Figure 1: Object bench with added reference to rectangle color

The dialog box hasn't closed after adding the reference. Why?

BlueJ expects that you would want use a second service of this dialog box – the ability to inspect the guts of the returned instance. However postpone exploration of this feature and utilize the open dialog box for a demonstration of the possibility to control an object through several references. Ask for another reference and name it e.g. RECTANGLE to remember, which shape has this color.

Nothing happened.

Why do you think so? Surely at the right border the scroll bar has appeared indicating that the object bench contains another line of references. While we won't use the class diagram for a while, you can drag the border between the class diagram and the object bench and draw it up a little. Hence you will see that BlueJ has added the reference into the next line.

Figure 2: Larger object bench

You are right. It is there. What shall we do with it?

You can send it e.g. the message

String getName()

and so ask it for the name of its color.

I asked the reference RECTANGLE for the name of the referred object and BlueJ opened the dialog box, where I got to know, that the name of the color is "red". I only wonder why this dialog box contains the button Get although it shows the returned value.

Figure 3: Dialog box with the RECTANGLE color name

The message you have sent asked for an object of type String that is an object type, however it has a special position among object types. Its instances are text strings, which mean sequences of characters that we take together as an object. Because we very often work with text strings, this type obtains some special privileges that facilitate the programmers' work.

We learn it further down the road. However now I am only revising, what we have already learned in this lesson. Run the accompanying animation and then go through the points of the following revision.

Click here to run annimation

Animation 1: Sending messages requesting return of reference to object

How to write strings

I would like see the animation, but first I would like to know, why the word red is closed inside quotations. So far all text was without quotations.

The quotations distinguish the values of instances of the class String from other texts in the program – e.g. from identifiers. Whenever you write a text that is a value of an instance of the class String, you have to close it inside quotation marks.

Can you show me the use of text string in an example?

We will start to use text stings after we learn how to send messages with parameters. We will learn this subject in the next lesson where I will show you everything and you can try it out.

Revision

  • We call the names of entities in programs (classes, objects, messages ') identifiers.
  • Type String is an object type that has some privileges over other object types.
  • Instances of the type String represent text strings.
  • Identifiers should comply with three rules:
  • They can contain only letters, digits and characters $ and _.
  • They must not begin with digit.
  • They must not have the same spelling as a keyword.
  • Java identifiers are case sensitive.
  • BlueJ enables us to modify the relative size of the class diagram and object bench by moving their common border.
  • We close the texts being values of instances of the class String inside quotations marks.
  • 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