Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

 

5.6 This and Super

 

Although the basic ideas of object-oriented programming are reasonably simple and clear, they are subtle, and they take time to get used to. And unfortunately, beyond the basic ideas there are a lot of details. The rest of this chapter covers more of those annoying details. Remember that you don’t need to master everything in this chapter the first time through. In this section, we’ll look at two variables, this and super, that are automatically defined in any instance method.

 

This and Super

 

5.6.1 The Special Variable this

 

What does it mean when you use a simple identifier such as amount or process() to refer to a variable or method? The answer depends on scope rules that tell where and how each declared variable and method can be accessed in a program. Inside the definition of a method, a simple variable name might refer to a local variable or parameter, if there is one “in scope,” that is, one whose declaration is in effect at the point in the source code where the reference occurs.

If not, it must refer to a member variable of the class in which the reference occurs. Similarly, a simple method name must refer to a method in the same class.

A static member of a class has a simple name that can only be used inside the class definition; for use outside the class, it has a full name of the form hclass-namei.hsimple-namei.

For example, “Math.PI” is a static member variable with simple name “PI” in the class “Math”. It’s always legal to use the full name of a static member, even within the class where it’s defined. Sometimes it’s even necessary, as when the simple name of a static member variable is hidden by a local variable or parameter of the same name.

Instance variables and instance methods also have simple names. The simple name of such an instance member can be used in instance methods in the class where the instance member is defined (but not in static methods).

Instance members also have full names—but remember that an instance variable or instance method is actually contained in an object rather than in a class, and each object has its own version. A full name of an instance member starts with a reference to the object that contains the instance member.

For example, if std is a variable that refers to an object of type Student, then std.test1 could be a full name for an instance variable named test1 that is contained in that object.

 

Chapter 5.6 - This and Super | Introduction to Programming Using Java

 

But when we are working inside a class and use a simple name to refer to an instance variable like test1, where is the object that contains the variable? The solution to this riddle is simple: Suppose that a reference to “test1” occurs in the definition of some instance method. The actual method that gets executed is part of some particular object of type Student.

When that method gets executed, the occurrence of the name “test1” refers to the test1 variable in that same object. (This is why simple names of instance members cannot be used in static methods—when a static method is executed, it is not part of an object, and hence there are no instance members in sight!)

This leaves open the question of full names for instance members inside the same class where they are defined. We need a way to refer to “the object that contains this method.” Java defines a special variable named this for just this purpose. The variable this can be used in the source code of an instance method to refer to the object that contains the method. This intent of the name, “this,” is to refer to “this object,” the one right here that this very method is in.

If var is an instance variable in the same object as the method, then “this.var” is a full name for that variable. If otherMethod() is an instance method in the same object, then this.otherMethod() could be used to call that method. Whenever the computer executes an instance method, it automatically sets the variable this to refer to the object that contains the method.

(Some object oriented languages use the name “self” instead of “this.” Here, an object is seen as an entity that receives messages and responds by performing some action. From the point of view of that entity, an instance variable such as self.name refers to the entity’s own name, something that is part of the entity itself. Calling an instance method such as self.redraw() is like saying “message to self: redraw!”)

One common use of this is in constructors. For example:

 

this and super

 

In the constructor, the instance variable called name is hidden by a formal parameter that is also called “name.” However, the instance variable can still be referred to by its full name, which is this.name. In the assignment statement “this.name = name”, the value of the formal parameter, name, is assigned to the instance variable, this.name.

This is considered to be acceptable style: There is no need to dream up cute new names for formal parameters that are just used to initialize instance variables. You can use the same name for the parameter as for the instance variable.

There are other uses for this. Sometimes, when you are writing an instance method, you need to pass the object that contains the method to a subroutine, as an actual parameter. In that case, you can use this as the actual parameter. For example, if you wanted to print out a string representation of the object, you could say “System.out.println(this);”. Or you could assign the value of this to another variable in an assignment statement.

You can store it in an array. In fact, you can do anything with this that you could do with any other variable, except change its value. (Consider it to be a final variable.)

 

Chapter 5.6 - This and Super | Introduction to Programming Using Java

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

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

Chapter 5.1.2 – Fundamentals of Objects | Introduction to Programming Using Java

Chapter 5.1.3 – Getters and Setters | Introduction to Programming Using Java

Chapter 5.2 – Constructors and Object Initialization | Introduction to Programming Using Java

Chapter 5.2.2 – Constructors | Introduction to Programming Using Java

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

 

21 thoughts on “Chapter 5.6 – This and Super | Introduction to Programming Using Java”

Leave a Comment