Chapter 5.1.1 – Objects, Classes and Instances | Introduction to Programming Using Java

Chapter 5.1.1 – Objects, Classes and Instances | Introduction to Programming Using Java

 

5.1.1 Objects, Classes and Instances

 

Objects are closely related to classes. We have already been working with classes for several chapters, and we have seen that a class can contain variables and subroutines. If an object is also a collection of variables and subroutines, how do they differ from classes? And why does it require a different type of thinking to understand and use them effectively?

In the one section where we worked with objects rather than classes, Section 3.8, it didn’t seem to make much difference: We just left the word “static” out of the subroutine definitions!

I have said that classes “describe” objects, or more exactly that the non-static portions of classes describe objects. But it’s probably not very clear what this means. The more usual terminology is to say that objects belong to classes, but this might not be much clearer. (There is a real shortage of English words to properly distinguish all the concepts involved.

An object certainly doesn’t “belong” to a class in the same way that a member variable “belongs” to a class.) From the point of view of programming, it is more exact to say that classes are used to create objects.

 

Classes and Instances

 

 

A class is a kind of factory for constructing objects. The non-static parts of the class specify, or describe, what variables and subroutines the objects will contain. This is part of the explanation of how objects differ from classes: Objects are created and destroyed as the program runs, and there can be many objects with the same structure, if they are created using the same class.

Consider a simple class whose job is to group together a few static member variables. For example, the following class could be used to store information about the person who is using the program:

 

Chapter 5.1.1 - Objects, Classes and Instances | Introduction to Programming Using Java

 

In a program that uses this class, there is only one copy of each of the variables UserData.name and UserData.age. There can only be one “user,” since we only have memory space to store data about one user. The class, UserData, and the variables it contains exist as long as the program runs. Now, consider a similar class that includes non-static variables:

 

Chapter 5.1.1 - Objects, Classes and Instances | Introduction to Programming Using Java

 

In this case, there is no such variable as PlayerData.name or PlayerData.age, since name and age are not static members of PlayerData. So, there is nothing much in the class at all— except the potential to create objects. But, it’s a lot of potential, since it can be used to create any number of objects! Each object will have its own variables called name and age.

There can be many “players” because we can make new objects to represent new players on demand. A program might use this class to store information about multiple players in a game.

Each player has a name and an age. When a player joins the game, a new PlayerData object can be created to represent that player. If a player leaves the game, the PlayerData object that represents that player can be destroyed. A system of objects in the program is being used to dynamically model what is happening in the game. You can’t do this with “static” variables!

In Section 3.8, we worked with applets, which are objects. The reason they didn’t seem to be any different from classes is because we were only working with one applet in each class that we looked at. But one class can be used to make many applets. Think of an applet that scrolls a message across a Web page.

There could be several such applets on the same page, all created from the same class. If the scrolling message in the applet is stored in a non-static variable, then each applet will have its own variable, and each applet can show a different message.

 

Chapter 5.1.1 - Objects, Classes and Instances | Introduction to Programming Using Java

 

The situation is even clearer if you think about windows, which, like applets, are objects. As a program runs, many windows might be opened and closed, but all those windows can belong to the same class. Here again, we have a dynamic situation where multiple objects are created and destroyed as a program runs.

An object that belongs to a class is said to be an instance of that class. The variables that the object contains are called instance variables. The subroutines that the object contains are called instance methods. (Recall that in the context of object-oriented programming, method is a synonym for “subroutine”.

From now on, since we are doing object-oriented programming, I will prefer the term “method.”) For example, if the PlayerData class, as defined above, is used to create an object, then that object is an instance of the PlayerData class, and name and age are instance variables in the object.

It is important to remember that the class of an object determines the types of the instance variables; however, the actual data is contained inside the individual objects, not the class. Thus, each object has its own set of data.

An applet that scrolls a message across a Web page might include a subroutine named scroll(). Since the applet is an object, this subroutine is an instance method of the applet. The source code for the method is in the class that is used to create the applet. Still, it’s better to think of the instance method as belonging to the object, not to the class. The non-static subroutines in the class merely specify the instance methods that every object created from the class will contain.

The scroll() methods in two different applets do the same thing in the sense that they both scroll messages across the screen. But there is a real difference between the two scroll() methods. The messages that they scroll can be different. You might say that the method definition in the class specifies what type of behavior the objects will have, but the specific behavior can vary from object to object, depending on the values of their instance variables.

As you can see, the static and the non-static portions of a class are very different things and serve very different purposes. Many classes contain only static members, or only non-static.

However, it is possible to mix static and non-static members in a single class, and we’ll see a few examples later in this chapter where it is reasonable to do so. You should distiguish between the source code for the class, and the class itself. The source code determines both the class and the objects that are created from that class.

 

Chapter 5.1.1 - Objects, Classes and Instances | Introduction to Programming Using Java

 

The “static” definitions in the source code specify the things that are part of the class itself, whereas the non-static definitions in the source code specify things that will become part of every instance object that is created from the class. By the way, static member variables and static member subroutines in a class are sometimes called class variables and class methods, since they belong to the class itself, rather than to instances of that class.

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 4.3.2 – Formal and Actual Parameters | Introduction to Programming Using Java

Chapter 4.3.3 – Overloading | Introduction to Programming Using Java

Chapter 4.3.4 – Subroutine Examples | Introduction to Programming Using Java

Chapter 4.3.5 – Throwing Exceptions | Introduction to Programming Using Java

Chapter 5 – Objects and Classes | Introduction to Programming Using Java

5 thoughts on “Chapter 5.1.1 – Objects, Classes and Instances | Introduction to Programming Using Java”

Leave a Comment