2017-03-09

In this article, we will cover some of the interview questions with their justification on this keyword in Java

These are most frequently asked interview question from this keyword in Java

Read this keyword in Java concepts in detail

Q) What is this keyword used in Java ?

thiskeyword in java is used to refer the current instance of the class

Q) Explain the usage of this keyword ?

Usage of this keyword in Java:

Instance variable:this keyword is used to refer the current instance variables of the class

Class constructor:this() constructor call; is used to invoke other overloaded constructor of the same class

Instance method:<methodName> is used to invoke current instance method of the same class

Method parameter:this keyword can be used to pass as argument in method invocation

Return type:this keyword can be used to return current class instance

Note:this cannot be used to refer anything in static context

Q) Whether it is possible to invoke one constructor from another constructor in the same class ?

Yes, it is possible to invoke one constructor from another constructor using this keyword

But this() ; constructor call must be 1st statement inside constructor

Otherwise, compile-time error will be thrown stating “Constructor call must be the first statement in a constructor”

Example, as shown in the below screen-capture

Q) How to invoke parameterized constructor from another constructor in the same class ?

Using this keyword, we can also pass constructor-arguments as shown in the below screen-capture

But this(arguments…) ; constructor call must be 1st statement inside constructor

Otherwise, compile-time error will be thrown stating “Constructor call must be the first statement in a constructor”

Syntax: this(arguments…);



Q) What will happen, if this() constructor call is present in last line of constructor ?

Whenever this(); constructor call present inside constructor to invoke another constructor, then it must be 1st statement

Otherwise, compile-time error will be thrown stating “Constructor call must be the first statement in a constructor”

Example, as shown in the below screen-capture

Q) Whether compiler inserts this(); constructor implicitly similar to super() constructor call ?

No, compiler doesn’t inserts this(); constructor call

Programmer can write either super() or this() constructor call as 1st statement

Else, if it isn’t explicitly coded, then compiler inserts super(); constructor call as 1st statement inside constructor

Q) How can we refer instance variables of same class ?

All member variables of same class can be referred using this keyword

Directly referring by variable name without using this will also work

Because after compilation, compiler inserts this keyword followed by dot notation and then actual variable name

Example, as shown in the below screen-capture

After compilation & de-compilation:

Q) Whether it is possible to refer static variables using this keyword ?

Yes, it is possible to refer static variable using this keyword

But its usage is discouraged as static variable belongs to class & it need to be accessed in static way

Syntax: <class-name>.<static-variable-name-of-class>

When we try to access using this keyword, then compiler warns with a message “The static field DemoExample.siteAge should be accessed in a static way”

Q) Whether it is possible to invoke instance methods using this keyword ?

All instance methods of same class can be invoked from another method using this keyword

But we invoke instance methods directly as well without using this keyword

Because after compilation, compiler inserts this keyword followed by dot notation and then instance method

Example, as shown in the below screen-capture

After compilation & de-compilation:

Q) What will happen, if static methods (non-instance) is invoked using this keyword ?

Whenever we try to invoke static using this keyword –> a compile-time error will be thrown stating “Cannot use this in a static context”

Example, as shown in the below screen-capture

Q) Whether it is possible to assign references to this keyword ?

Assigning any object reference to this keyword results in compile-time error stating “The left-hand side of an assignment must be a variable”

Example, as shown in the below screen-capture

Q) Whether it is possible to return this (this keyword) ?

Yes, this keyword can be used to return current class instance

Note:To return this keyword (current instance) from method, we need to have return-type as current class-type; otherwise it won’t work

Q) Whether it is possible to pass this as method arguments ?

Yes, thiskeyword can be used to pass as argument in method invocation

Example, as shown in the below careen-capture

Q) What will happen, if this keyword is used inside static blocks or static methods ?

Assigning references or accessing variables or invoking instance methods using this keyword from static contexts i.e.; within static block or static methods results in compile-time error stating “Cannot use this in a static context”

Let us see two example for these cases

Case 1: accessing instance variable from static block

Case 2: invoking instance method from static method

Case 1: accessing instance variable from static block

Case 2: invoking instance method from static method

Q) In which scenarios, this and super will be used ?

Whenever we want to access/refer a variable/constructor/method of the same class, then this keyword can be used

Similarly, whenever we want to access/refer a variable/constructor/method of the immediate parent class, then super keyword can be used

Refer this keyword in Java for more details

Refer super keyword in Java for more details

References:

https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
http://www.benchresources.net/this-keyword-in-java/

Happy Coding !!

Happy Learning !!

Show more