Docsity

Java Keyword - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Java Keyword - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

The Java Keyword this

Instance Methods

  • First we introduce instance methods:
    • any method not declared with a static keyword
    • operates on an instance of the class (an object)

instead of operating on the class itself

  • this object instance is often referred to as the

receiving instance of the instance method

Using Instance Methods

  • To use an instance method from outside of the class in which it is defined we must prepend a reference to the instance that is to be operated on: //create a Circle object and store in c Circle c = new Circle(); c.r = 2.0; //set an instance field of object //invoke an instance method of the object double a = c.area(); Notice the method does not have a parameter. How does it know what data to operate on?

The Object Reference this

  • All instance methods are implemented with

an implicit parameter not shown in the method signature.

  • The implicit argument is named this
  • It holds a reference to the object through

which the method is invoked. In our example, that object is a Circle.

Accessing Shadowed Fields

  • In some cases a field in a class can be

shadowed or hidden inside a method by a parameter or a local variable by the same name.

  • For example, when a method parameter has

the same name as one of the fields of the class, you must use this to refer to the field.

Accessing Shadowed Fields

  • Looking at our previous example class Circle, we add the method setRadius(). The local variable r shadows the instance field r. We must use this.r to refer to the instance field set to argument r of the method setRadius().

Invoking other Constructors of Same

Class

  • A class can define two or more overloaded constructors with same name and different argument lists.
  • Sometimes it is useful for one overloaded constructor to invoke another in the same class.

Invoking other Constructors of Same

Class

  • In our given example class calcData , the statement

this(30); //line number 12

is used by an overloaded constructor public calcData()//line no. 10; also //called a noarg //constructor (no parms.) to invoke another overloaded constructor while passing an integer value of 30 as a parameter.

Passing this as a Parameter

  • When one object invokes a method in another object and

passes a reference to itself as a parameter (also referred to as registration).

  • The method in the second object saves the reference that it

receives as an incoming parameter.

  • This makes it possible for a method in the second object to

make a callback to the first object later when necessary.

Passing this as a Parameter

  • To illustrate: Department d contains a method ( newFaculty() ) which invokes a method belonging to Faculty a passing a reference to itself as a parameter (line 26).

Why use this?

  • To summarize:
    • To provide more information, more clarity.
    • To distinguish between fields and local/

parameters variables of the same

  • To make it possible for one overloaded

constructor to invoke another overloaded

constructor in the same class.

  • To pass a reference to the current object to a

method belonging to a different object.