This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CSE115 Course Notes 2009 C. Alphonce and A. Decker Page 28 Lesson 11: Naming Objects (Variables and References) One of the first things two people exchange when they meet is names. People do this in order to have a way of referring to each other. In fact, naming is so useful that people give names to many things in their environment, not only people. Pets are given names, as are cars, houses, even pieces of furniture. Once an object has been named it can be referred to and talked about. Building a model involves more than just identifying objects. We said early on in our study of object oriented modeling that object oriented models consist of collections of interacting objects. Remember that objects interact with each other by sending messages to each other. In order for one object to send a message to another object, the object sending the message must be able to refer to the object that the message is destined for. Our ability to build an object oriented system is severely limited by our inability to refer to an object after it has been created. We have seen how to create an object and invoke one of its capabilities. An example we showed in an earlier chapter was the following: new example1.Terrarium().add(new example1.Ant()) We know that evaluating this expression results in two new objects being created: a Terrarium object and an Ant object. The add capability of the Terrarium adds the object it is given in its argument list to the Terrarium environment, so that the Ant appears within the graphical representation of the Terrarium. Suppose we wanted to have more than one Ant in the Terrarium at a given time. With what we know so far about programming languages, we cannot do this! We can certainly create more Ant objects each time we evaluate the expression new example1.Ant() a new Ant object is created, so thats not the problem. The problem is that we cannot keep adding Ant objects to the Terrarium object we already have, because we have no way of referring to that Terrarium object more than once. In particular, we cannot repeat the same expression, as in, new example1.Terrarium().add(new example1.Ant()) because then yet another Terrarium is created to hold the new Ant, rather than putting the Ant into the Terrarium we already have (try it)! In effect this expression says create a new Ant object and put it into a new Terrarium object. What we want to be able to say is create a new Ant object and put it into the Terrarium object we created before. To express the Terrarium object we created before we need a name. Let us suppose we were able to name the Terrarium object we created in the first expression ourFirstTerrarium. In this case we could achieve what we want by writing ourFirstTerrarium.add(new example1.Ant()) So what can we do? In order to be able to refer to one and the same object across several method invocations, we need to name that object. In programming languages, like Java, we use a variable to associate a name with an object reference. use a variable to associate a name with an object reference....
View Full Document
- Fall '08