Rabu, 06 Juli 2016

Java interview programming questions on this keyword part 2



Java Quiz on this keyword part #2
  • Java interview program on this keyword: can we call method using this keyword?
  
Program  #5:

java programming on this keyword





Program #6: Can we call non static method from constructor using this?

Java programming examples on this keyword





Program #7: Can we assign something to this ?



  1. package thiskeywordinterviewprograms.java;
  2. public class ThisDemo {
  3.  
  4.     int a;
  5.     int b;
  6.     
  7.  ThisDemo(int x, int y){
  8.  
  9.        this= new ThisDemo();
  10.         System.out.println("Two argument constructor called.");
  11.         this.x=x;
  12.         this.y=y;
  13.         
  14. }
  15.  
  16. public static void main(String[] args) {
  17.         
  18.     ThisDemo obj = new ThisDemo(10, 20);
  19.         
  20.      System.out.println(obj.a);
  21.      System.out.println(obj.b);

  22. }
  23.  
  24. }





Program #8 : Can we use this as return statement in a method?


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





Bagikan

Jangan lewatkan

Java interview programming questions on this keyword part 2
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.