» » » Top 10 Java interview questions and programs on this keyword

1.What is this key word in java?

  • "this"is a predefined instance variable to hold current object reference

2.What are the uses of this keyword in constructor?



1.this must be used to access instance variable if both instance and local variable names are same.

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.       a=a;
  9.       b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 0
  2. 0


using this keyword:

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.       this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 1
  2. 2


  • We can use this keyword in constructor overloading. 
  • To call one constructor from another we need this(); and this(); call should be first statement of the constructor.

2.Used to invoke current class constructor:

 

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6. ThisDemo(){
  7. System.out.println("Default constructor called");
  8. }

  9.  ThisDemo(int a, int b){
  10.        this();
  11.         this.a=a;
  12.        this.b=b;
  13.  }
  14.  
  15.     public static void main(String[] args){
  16.  
  17.         ThisDemo obj= new ThisDemo(1,2);
  18.         
  19.         System.out.println(obj.a);
  20.         System.out.println(obj.b);
  21. }
  22. }

Output:

 

  1. Default constructor called
  2. 1
  3. 2


3. Can we call methods using this keyword?

  • Yes we can use this keyword to call current class non static methods .

  1. package com.instanceofjava;
  2.  
  3. public class Test{
  4.     int a, b;
  5.  

  6.  Test(int a, int b){
  7.       
  8.        this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12. void show(){
  13.  
  14. System.out.println("Show() method called");
  15.    
  16. }
  17.  
  18. void print(){
  19.  
  20.     this.show();
  21.     System.out.println(a);
  22.     System.out.println(b);
  23.  
  24.  }
  25.     public static void main(String[] args){
  26.  
  27.         Test obj= new Test(1,2);
  28.         
  29.       
  30.         obj.print()
  31. }
  32. }

Output:

 

  1. Show() method called
  2. 1
  3. 2

3. Can we call method on this keyword from constructor?

  • Yes we can call non static methods from constructor using this keyword.

this keyword in java interview questions for freshers




4.Is it possible to assign reference to this ?

  • No we can not assign any value to "this" because its always points to current object and it is a final reference in java.
  • If we try to change or assign value to this compile time error will come.
  • The left-hand side of an assignment must be a variable
this keyword in java with example program
5.Can we return this from a method?

  • Yes We can return this as current class object. 

  1. public class B{

  2.    int a;
  3.     
  4.  public int getA() {
  5.         return a;
  6.  }
  7.  
  8. public void setA(int a) {
  9.         this.a = a;
  10. }
  11.  
  12. B show(){
  13.     return this;
  14. }
  15.  
  16. public static void main(String[] args) {
  17.        
  18.  B obj = new B();
  19.  
  20.   obj.setA(10);
  21.  
  22.  System.out.println(obj.getA());
  23.  B obj2= obj.show();
  24.  System.out.println(obj2.getA());
  25.  
  26. }

  27. }

Output:

  1. 10
  2. 10

6.Can we pass this as parameter of method?

  • Yes we can pass this as parameter in a method

7. Can we use this to refer static members?

  •  Yes its possible to access static variable of a class using this but its discouraged and as per best practices this should be used on non static reference.

8.Is it possible to use this in static blocks?

  •  No its not possible to use this keyword in static block.

use this keyword in static block


9.Can we use this in static methods? 

  • No we can not use this in static methods. if we try to use compile time error will come:Cannot use this in a static context

10.What are all the differences between this and super keyword?

  • This refers to current class object where as super refers to super class object
  • Using this we can access all non static methods and variables. Using super we can access super class variable and methods from sub class.
  • Using this(); call we can call other constructor in same class. Using super we can call super class constructor from sub class constructor.
11. write a java program on constructor overloading or constructor chaining  using this and super keywords

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

13 comments for Top 10 Java interview questions and programs on this keyword

Select Menu