Broad Network


The this Reserved Word in Java

Java Object Oriented Programming Core – Part 5

Forward: In this part of the series I explain to you the meaning of the Java reserved word, this.

By: Chrysanthus Date Published: 3 Sep 2012

Introduction

This is part 5 of my series, Java Object Oriented Programming Core. A reserved word in a computer language is a word that you cannot use arbitrarily. It has a special meaning in the language. Java is case sensitive, so all its reserve words are case sensitive. In this part of the series I explain to you the meaning of the Java reserved word, this.

Meaning of the this Reserved Word
The reserved word, this, is a reference to the instantiated object whose member method is being executed. So, inside a method, you can use the "this" reserved word as the instantiated object variable to access a property of the same object or to access another method of the same object. The following code gives an illustration for accessing a property:

class MyClass
    {
        int num1;

        void assignShow()
            {
                this.num1 = 12;
                System.out.println(num1);
            }
    }

class ThisReserved
    {
        public static void main(String[] args)
            {
                MyClass myObj = new MyClass();
                myObj.assignShow();
            }
    }

In Java, the variable of an instantiated object is a reference: meaning it refers to the object and does not replace the object. The reserved word, this, is predefined in Java. It is a reference to the object whose method is being executed. It has to be coded in the class method of interest. Any method has its time of execution in an object.

The referenced object here is the object instantiated from a class, whose method is in the process of execution. As the method is being executed, using the word, this, would refer to the method’s object. That is how the Java class was designed to work.

In the above code, the method assignShow() uses the reserved word, this, which is the reference to its object (the object is instantiated from the class). Line 1 in the method uses “this” to access the property, num1. It was not very necessary just to assign a value to a property this way, as we could have used num1 directly, because a method can access a property of the same class (object). However, it was done to illustrate the use of the “this” reserved word. Note the use of the dot operator with the “this” reserved word. The dot operator is the operator to use to join a class object reference and the object property (or method) in code. Read and try the above code.

The following example illustrates the use of the “this” reserved word for a method (called aMethod). Read and try it:

class MyClass
    {
        int num1;

        void aMethod()
            {
                System.out.println("I am alive!");
            }

        void assignShow()
            {
                this.aMethod();
            }
    }

class ThisReserved2
    {
        public static void main(String[] args)
            {
                MyClass myObj = new MyClass();
                myObj.assignShow();
            }
    }

So, “this” is used to access the properties and methods in a method of the object under execution. "this" goes into action when the method in which it is coded is operating.

Why not use an instantiated object variable, in place of, this? Answer: Normally you code “this” in a class declaration (description). Many objects have to be instantiated from that same class. You may not know the variable name of a future instantiated object while coding the class. So “this” would represent any future variable (name).

Well, let us end here and continue in the next part of the series.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message