The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 10 Java interview questions and programs on this keyword

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 10 Java interview questions and programs on this keyword Posted: Mar 9, 2016 11:41 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 10 Java interview questions and programs on this keyword
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
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.It must used explicitly if non-static variable and local variable name is 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

  • We can use this keyword useful in constructor overloading i.e to call other constructor from a constructor in a class

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(obj.a);
  22.     System.out.println(obj.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 on this keyword?

  • 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.

    Read: Top 10 Java interview questions and programs on this keyword

    Topic: Docker Flow: Blue-Green Deployment and Relative Scaling Previous Topic   Next Topic Topic: LambdaNative brings functional programming to IoT

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use