Home > Creating and using objects > Creating and using objects in Java

Creating and using objects in Java

 

Module 2: Creating and using objects.

 

Objectives:

  • Declaring object references, instantiating objects, and intializing object reference variables.
  • Compare how object reference variables are stored in relation to primitive variables.
  • Use a class (the String class) included in the Java SDK.
  • Use the J2SE class library specification to learn about other classes in this API.

 

In the previous module we discussed about primitive data types and how to use them with variables. In this module we will be focussing about objects and object reference variables. We will learn how object reference variables are stored in memory with reference to local variables. We will also take a look into the String class, as well as the class library specification to know about the other classes that we have access to.

 

1. Declaring object references, instantiating objects, and intializing object reference variables:

In the previous module we were dealing with primitive data types. And when we declared a variable of  a primitive datatype, we stored the primitive in the variable itself. But Java is object oriented language, so we really want to be dealing with objects. While dealing with objects you will also need a variable associated with it. But it works different with the objects. You dont store an object inside of a variable like you do with the primitives. What you do is, you store a reference to the object inside the variable.

Now a reference is like an address to a house. It allows you to find that object. So using that reference we can call up that object and ask it to do something. We can either access its attributes, or we can access its methods. Just like we can send a letter to a house as we have its address, we can access an object using its reference.

 

object reference

 

 

 

Example: Declaring object references:

Here we have the ShirtTest class that we used in the previous module to create a Shirt object and then access it. In the following sections we will take a detailed look at the creation of myShirt object refernce variable and the creation of the Shirt object and how these two are tied together and how the myShirt object reference variable is used to access the Shirt object.  

 

 ShirtTest class

 

 

Declaring object reference variables:

The first thing you need to make sure you have when you are creating an object, is a place to store the reference to the object. As you can see in the figure below, in line number 6 of our ShirtTest class in the main method, we are creating a variable called myShirt of type Shirt. Now this does not create a Shirt object, what it does is it just creates a reference variable that can hold utmost one reference at a time, to an object of type Shirt. You have to know that you will not have a Shirt object but just have a reference to a Shirt object. And using that reference we can access the object’s attributes or call its methods.

Declaring object reference variable

 

 

Remember a reference is like an address to a house, it allows us to find that object in memory and access it.  You can see what happens in the memory when this line of code executes. We get a myShirt variable allocated in stack memory which can hold utmost one reference at a time to a Shirt object.

 

Instantiating an object:

So once we have our object reference variable created that can hold the reference to our object, we can then create the object using the (new) operator. We are issuing a command called new Shirt(); , this will create the Shirt object. Now from before we know that objects are allocated in heap memory as we can also see in our figure below. Also, the attributes of the object are allocated in heap memory along with the object itself.  Now the new operator has a return value. Its return value is the reference to the object that it has just created. So, we need to assign this return value to our variable  myShirt.

 

Instantiating an object

 

 

Initializing object reference variables:

So in this figure, you can see that the new operator is returning the Shirt object’s reference and we are storing teh returned reference in the myShirt reference variable. This creates a sort of a link  between the myShirt variable and the Shirt object itself. And then, using this reference we can access the object’s attirubutes like shirtID, description, colorCode etc, or access its methods such as displayShirtInformation().  

 Initializing object reference variables

 

Using an object reference variable to manipulate an object’s attributes:

Now in this figure, you can see that we are using the reference stored in the myShirt variable along with the (.) dot notation to access the colorCode attribute of the Shirt object. And we can change the colorCode from ‘U’ to ‘G’ (or anything you wish). Once you have a reference to an object stored in your reference variable, you can then use the dot notation to access its attributes and either read them or write to them or to invoke the object’s methods.  

 Using an object reference variable to maipulate an object's attributes

 

 

 

2. Compare how object reference variables are stored in relation to primitive variables:

Let’s take a look at much more in-depth example to make sure that we know what an object is and what an object reference variable is and how these 2 co-relate to one another. In the below figure, you can see that in line 4 we are creating a myShirt variable which we had in the previous example and the Shirt object from the previous example. The reference address is stored in the myShirt variable which creates a link to the Shirt object. In line 5, we are creating a different reference variable called yourShirt, and also creating a different Shirt object using the new operator, and we are tying these 2 togther by storing the reference of the newly created Shirt object in the yourShirt variable.  

Compare how object reference variables are stored in relation to primitive variables

 

So, we have a myShirt variable pointing to one Shirt object and a yourShirt variable pointing to another Shirt object. Then, we are using the myShirt reference variable to call the displayShirtInformation() method on the first Shirt object as these 2 are linked with each other. We are also calling the displayShirtInformation() method on the yourShirt object. And then, we are going to change the colorCode attribute on both of these objects.  For that, we are going to change the colorCode of  myShirt object to ‘R’ and yourShirt object’s to ‘G.

Notice again the use of the object reference variable and the dot notation to access respective object’s attributes. And then, lastly we recall the displayShirtInformation() method for both of those objects to show that the myShirt object now has ‘R as its colorCode and yourShirt object has ”G’.

 

Assigning a reference from one variable to the other:

As discussed previously, an object reference variable can store upto utmost one reference to an object at a time. The reference variable may not store any reference at all but it can store upto one utmost. Even though a reference variable can only store one object at a time, this does not mean that an object cannot have more than one reference variable referring to it at the same time. It is also the case that an object reference variable does not have to store the same reference to the same object throughout its lifetime. It can refer to one object at one time, and an other object at another time by changing its object reference value. Let’s take a look at both of these scenarios:  

assigning a reference

 

So, we have seen that a reference variable can change its value in pointing from one object to another. And now we also have a situation where we have 2 variables referring to the same object. So lets see what happens in this situation. Let’s use the myShirt object reference variable to change the object’s colorCode to an ‘R’. Doing this will not only change the object’s colorCode thats referred by the myShirt variable to ‘R’ but also the object’s colorCode thats referred by the yourShirt variable to ‘R’. This is because both the variables are pointing to the same object and the object’s attributes can be accessed and modified using any variable pointing to it.

Now, we will use the yourShirt variable to change the colorCode of the object associated with it to a ‘G’. What we are now doing is, we are overriding the ‘R’ that we set to the colorCode of the object using myShirt variable, since both yourShirt and myShirt refer to the same object. So it doesnt matter now which reference variable you use, you will be updating the same object.

 

 

 3. Use a class (the String class) included in the Java SDK:

Now lets take a look at the String class. The String class is confused with a primitive, but its not (recollect the primitives we discussed in earlier module).  String is a class in Java programming language, and so we are dealing with objects when we use String in our code. It is confused with a primitive because how it operates, such as you do not have to use a new operator to create a new String object. Now with all other objects you do have to use a new operator to create one. But this is an exception in Java, because we use String so often in our code. 

 

 Use a class (the String class) included in the Java SDK

 

Notice in the upper window, we are just putting Fred Smith in quotes. Java will then create a String object associated with this without having to create a new object using the new operator. As we see in the lower window, you can use the new operator to create a new String but this is redundant and not necessary. So lets see how String objects are kept in memory.

Since a String object is an object and not a primitive, we have the same situation we had before. We will have an object reference variable that holds the reference to the object. So in this case the object reference variable is called myName. It holds the reference to a String object which is allocated in heap memory. We are also showing the actual characters Fred Smith which are kept in another portion of heap memory which has a reference to it from the String object itself.

So you can see that a String object is like any other object. Infact when you set one String object to another String object, you are just passing object references from one variable to another just like we saw in the previous section.

 

Using reference variable for String objects:

Another reason that some people think that a String object is a primitive besides the fact that you dont have to use a new operator is that it acts like a primitive with use of a (+) plus symbol. With most of the other primitives, when we use the + symbol, we are doing addition. So if we do 3 + 4, we get 7 :). But when you use it with Strings, you are doing String concatenation 

 Using reference variable for String objects

So, in this example, we have an attribute variable named ‘name’ which is equal to ‘Jonathan’, and another attribute variable namely ‘job’ which is equal to ‘Ice Cream Taster’ and then in the display method for the object, we are printing out a String which reads ‘My name is ‘ and then concatenating the name variable + ‘, I am a’ and concatenating the job variable. In this case we are doing String concatenation. We will get ‘My name is Jonathan, I am a ‘Ice Cream Taster’.

So, the fact that you dont have to use the new operator to create a String object and you can use a + operator to do String operator leads some people to believe that its a primitive. Because it has primitive like qualities to it.  But it is an object and you are creating object reference variables when you create String variables.

 

 

4.  Use the J2SE class library specification to learn about other classes in this API:

As you explore Java, you are going to encounter many many classes. And many times you are going to need to find information about some classes, such as what attributes they have, what methods they have, what the constructors are for those classes, and for all these you will like to refer to Java documentation API. You can either access it online, or you can download it from Sun website.  If you wish to access it online and take a look around, you can do so as well from the Sun site.(click here).

As you look around the API documentation, you can see that the browser screen is broken into 3 frames. In the upper left hand frame, are your packages (We haven’t discussed this as yet, but you can consider them for now as logical grouping of classes to help organise things). So, if you click one of the packages in the upper left hand frame, it will list the classes in that packages in the lower left hand frame.

And then when you click on one of the classes in the lower left hand frame, the class will be displayed in the right hand frame. This will tell you infomation such as, what package the class belongs to, what class it inherits from, what interfaces the class implements, some information about the class and how to use the class, what the constructors are for the class, what the attributes are, and what the methods are. So you will have access to a wealth of information. You don’t have to remember all the classes, and all its attributes or methods as you may know by looking at the API :). It is just a reference to all the classes in Java that you may need information about.   

 

Note:

  • Many new to Java, often confuse with the use of the terms such as variable, attribute / instance variable, local variable, reference variable, object reference variable etc. Note that it in many cases these terms are used interchangibly.
  • Once you are familiar with each of these terms, you should not be confused with the way these terms are used. For instance, reference variable is termed so as it holds reference to an object, and it is a variable as it can vary or its contents can vary. In this case, a reference variable can point to any object and hence its a variable.
  • Similarly, the term object reference variable is nothing but a reference variable but spelled completely. Sometimes, many of us are lazy enough to type the whole big word so we use the short cut word, but otherwise they mean the same.
  • When it comes to attribute / instance variable, any variable (primitive type or Object type) can be a attribute variable when it is declared in the class definition and belongs to the objects of a class.

 

 

Note:

 

Source: Sun Microsystems Educational Services Course – Beginning to Program with the Java Programming Language WJ-1102A.

 

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: