1.What is this key word in java?
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.
using this keyword:
3. Can we call methods using this keyword?
3. Can we call method on this keyword from constructor?
4.Is it possible to assign reference to this ?
6.Can we pass this as parameter of method?
7. Can we use this to refer static members?
8.Is it possible to use this in static blocks?
9.Can we use this in static methods?
10.What are all the differences between this and super keyword?
- "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.
- package com.instanceofjava;
- public class ThisDemo {
- int a, b;
- ThisDemo(int a, int b){
- a=a;
- b=b;
- }
- public static void main(String[] args){
- ThisDemo obj= new ThisDemo(1,2);
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:
- 0
- 0
using this keyword:
- package com.instanceofjava;
- public class ThisDemo {
- int a, b;
- ThisDemo(int a, int b){
- this.a=a;
- this.b=b;
- }
- public static void main(String[] args){
- ThisDemo obj= new ThisDemo(1,2);
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:
- 1
- 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:
- package com.instanceofjava;
- public class ThisDemo {
- int a, b;
- ThisDemo(){
- System.out.println("Default constructor called");
- }
- ThisDemo(int a, int b){
- this();
- this.a=a;
- this.b=b;
- }
- public static void main(String[] args){
- ThisDemo obj= new ThisDemo(1,2);
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:
- Default constructor called
- 1
- 2
3. Can we call methods using this keyword?
- Yes we can use this keyword to call current class non static methods .
- package com.instanceofjava;
- public class Test{
- int a, b;
- Test(int a, int b){
- this.a=a;
- this.b=b;
- }
- void show(){
- System.out.println("Show() method called");
- }
- void print(){
- this.show();
- System.out.println(a);
- System.out.println(b);
- }
- public static void main(String[] args){
- Test obj= new Test(1,2);
- obj.print()
- }
- }
Output:
- Show() method called
- 1
- 2
3. Can we call method on this keyword from constructor?
- Yes we can call non static methods from constructor using this keyword.
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
- Yes We can return this as current class object.
- public class B{
- int a;
- public int getA() {
- return a;
- }
- public void setA(int a) {
- this.a = a;
- }
- B show(){
- return this;
- }
- public static void main(String[] args) {
- B obj = new B();
- obj.setA(10);
- System.out.println(obj.getA());
- B obj2= obj.show();
- System.out.println(obj2.getA());
- }
- }
Output:
- 10
- 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.
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.
great job sir what A beautiful and very useful website thanks a lot
ReplyDeleteits very nicely described..Thank you so much .
ReplyDeleteExtradinary........
ReplyDeletesuperb explanation with examples
ReplyDeleteGreat work.. Thanks a lot!!
ReplyDeletesuper ...
ReplyDeletenice ...
ReplyDeletenice ...
ReplyDeleteSuperbly explained, specially by programming.
ReplyDeleteNice post..............
ReplyDeletenice tutorial
ReplyDeleteReally Helpfull....
ReplyDeleteNicely Described...!!
ReplyDeleteIn question 3: We can call both, static and non-static methods in constructor using 'this' keyword.
ReplyDelete