Use of this keyword in Java

In this post, you will see in detail explained the use of this keyword. Do try all examples by yourself. Let's check out it below,

Use of this keyword

this keyword is used to refer current object. There are several uses of ' this '

• this keyword can be used to refer to the constructor: generally, constructors are called explicitly using this keyword to reinitialize the current object. 

Syntax: this(parameter1, parameter2,.....); 

this keyword can be used to refer to a current instance variable: generally is used to differentiate between local variable and instance variable also between the current instance variable and another object instance variable. 

Syntax: this.variable name 

this keyword can be used to refer current instance method 

Syntax: this.method name

Example: use of this keyword for object reinitialization

use of this keyword for object reinitialization

 

public class Rectangle {

       int length;

       int breadth;

       public Rectangle() {

             this(34, 35);

       }

       public Rectangle(int length, int breadth) {

             this.length = length;

             this.breadth = breadth;

       }

       public int area() {

             int ar = length * breadth;

             return ar;

       }

       public static void main(String args[]) {

             Rectangle rect = new Rectangle(); 

        // this call will go to zero constructor

             Rectangle rect1 = new Rectangle(23, 56); // this call will go to                 parameterized constructor

             System.out.println("rect object length and breath is: " + rect.length                 + " " + rect.breadth);

             System.out.println("rect1 bject length and breath is: " + rect1.length                 + " " + rect1.breadth);

             int area1 = rect.area(); // calling instance method

             System.out.println("Area of rect :" + area1);

             int area2 = rect1.area(); // calling instance method

            System.out.println("Area of rect1 :" + area2);

       }

}

 


Output

rect object length and breath is: 34 35

rect1 bject length and breath is: 23 56

Area of rect :1190

Area of rect1 :1288


Parameter Passing :

The parameters can be passed by either

• Call by value 

• Call by reference

Passing Parameters by Value: 

While working under the calling process, arguments is to be passed. These should be in the same order as their respective parameters in the method specification. Passing Parameters by Value means calling a method with a parameter. Through this, the argument value is passed to the parameter. An example is shown above.

Passing objects: 

In java, objects are implicitly passed by 'call by reference', but no need of & operator

Java instanceOf operator: 

The Java instanceof operator is used to test whether the object is an instance specified type (class or subclass or interface). 

The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has null value, it returns false.

Example: 

Following is the simple example of instance operator where it tests the current class.

 

public class Simple {

       public static void main(String args[]) {

             Simple s = new Simple();

             System.out.println(s instanceof Simple);// true

       }

}


Output

true

instanceof with reference variable :

An object of subclass type is also a type of parent class. For example, if Dog extends Animal then object of Dog can be referred by either Dog or Animal class.

Example: 

 

class Animal {

}

public class Dog extends Animal { // Dog inherits Animal

       public static void main(String args[]) {

             Animal d = new Dog();

             System.out.println(d instanceof Animal); // true}

       }

}

Output

true


instanceof with a variable that have null value :

If we apply instanceof operator with a variable that have null value, it returns false.

Example: 

 

public class Simple {

       public static void main(String args[]) {

             Simple s = null;

             System.out.println(s instanceof Simple); // false

       }

}


Output

false


Recommend Topics:

Core Java

Oops Features

String Handling

Exception Handling 


Newest
Previous
Next Post »

ConversionConversion EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng