Tuesday, November 13, 2018

JAVA this Keyword

Example of this keyword in Java



Image result for java this keyword image



This is a java keyword, It can be used to represent current class object.Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
In Java Application, we are able to utilize this keyword in the following 4 ways.

1. To refer current class variable
2. To refer current class constructor
3. To refer current class method
4. To refer current class object

1. To refer current class variable

If we want to refer current class variables by using this keyword then we have to use the following syntax:

this.var_name

Note:
In Java application, if we have the same set of variable at local and at class level and if we access
that variable then JVM will give priority for local variable, if local variables are not available then JVM will search for that variables at class level,evn at class level also that variable are not available then JVM will search for at super class level. At all the above locations, if the specified variables are not available then compiler will rise an error.


Note:
In Java application if we have same set of variables at local and at class level then to access class level variables over local variables we have to use this keyword.




package com.dev.praticecollection;

class A
{
     int i=20;
     int j=20;
     A(int i,int j){
           System.out.println(i+":"+j);
           System.out.println(this.i+":"+this.j);
     }
}

public class Test {

     public static void main(String[] args) {
           A a= new A(100,200);

     }

}



 Output:
100:200
20:20

Real time utilization of this keyword while accessing class level variables. In general, in enterprise application, we are able to write no of java bean class as per application requirement. In Java bean classes, we are able to provide variables and their setter methods and getter methods.

In java bean classes, in setter method, always we have to assign local variable to class level variable. In this context, to refer class level variables over local variable we have to use this keyword.
In getter methods, always we have to return class level variable but it is not required to use this keyword, because in getter method no local variable.





package com.dev.praticecollection;

public class User {
     private String uname;
     private String upwd;
     public String getUname() {
           return uname;
     }
     public void setUname(String uname) {
           this.uname = uname; //class level=local variable
     }
     public String getUpwd() {
           return upwd;
     }
     public void setUpwd(String upwd) {
           this.upwd = upwd;
     }
    

}




2. To refer current class method

If we want to refer current class method by this keyword then we have to use the following systax:

this.method_name([param_list]);





package com.dev.praticecollection;

class A
{
     void m1()
     {
           System.out.println("M1-A");
     }
    
     void m2()
     {
           System.out.println("M2-A");
           m1();
           this.m1();
     }
}

public class Test {

     public static void main(String[] args) {
           A a= new A();
           a.m2();

     }

}

 
Output:
M2-A
M1-A
M1-A



3. To refer current class constructors

If we want to refer current class constructors by this keyword then we have to to use following syntax:

this([param_list]);




package com.dev.praticecollection;

class A {
     A() {
           this(10);
           System.out.println("A-0-arg-constructor");
     }

     A(int i) {
           this(22.22f);
           System.out.println("A-int-arg-constructor");
     }

     A(float i) {
           this(10.0);
           System.out.println("A-float-arg-constructor");
     }

     A(double i) {

           System.out.println("A-double-arg-constructor");
     }
}

public class Test {

     public static void main(String[] args) {
           A a = new A();

     }

}




 Output:

A-double-arg-constructor
A-float-arg-constructor
A-int-arg-constructor
A-0-arg-constructor




NOTE: 
In the above program, we have provided more than one constructors with the same name and different parameter list, this process is called as "Constructor overloading".
In the above program, we have called a the current class constructor by this keyword in chain fashion this process is called as Constructor chaining

NOTE:
If want to access current class constructor by using this keyword, then the respective this element must be provided as first statement.

A()
{
S.O.P(“TEST”); (Incorrect )
This(10);
}


NOTE:

If want to refer current class constructor by this keyword then the respective this statement must be provided in the current class another constructor, not from normal java method.
If we violate the above two rules then compiler will rise an error like ‘call to this must be first statement in constructor.

NOTE:

Due to the above reasons, we are able to access only one current class constructor by this keyword from another current class constructor, it is not possible to access more than one current class constructors by using this from a single current class constructor.





class A {
     A() {
           this(10);
           this(22.22f) //Invalid
           System.out.println("A-0-arg-constructor");
     }
    
}




 Example 2:



package com.dev.praticecollection;

class A {
     A() {
           this(10);
           System.out.println("A-0-arg-constructor");
     }

     A(short s) {

           System.out.println("A-short-arg-constructor");
     }

     A(int i) {

           System.out.println("A-int-arg-constructor");
     }

     A(long l) {

           System.out.println("A-long-arg-constructor");
     }

     A(double d) {

           System.out.println("A-double-arg-constructor");
     }

}

public class Test {

     public static void main(String[] args) {
           A a = new A();

     }

}


Output:
A-int-arg-constructor
A-0-arg-constructor



In the above example, when JVM encounter this(10) then JVM will search for a constructor having matched parameter, if it is not available then JVM will search for a constructor having next higher data type operator.

In the above example, JVM will search for int parametrized constructor for this(10) constructor call, if it is not available then JVM will search for long parametrized constructor, if it is not available then JVM will search for float parametrized and double parametrized constructor in the same class.


4.To refer current class object 


package com.dev.praticecollection;

class A {
     A getRef1() {
           A a = new A();
           return a;
     }

     A getRef2() {
           return this;
     }

}

public class Test {

     public static void main(String[] args) {
           A a = new A();
           System.out.println(a);
           System.out.println("---------------");
           System.out.println(a.getRef1());
           System.out.println(a.getRef1());
           System.out.println("---------------");
           System.out.println(a.getRef2());
           System.out.println(a.getRef2());

     }

}



 Output :
com.dev.praticecollection.A@52e922
---------------
com.dev.praticecollection.A@25154f
com.dev.praticecollection.A@10dea4e
---------------
com.dev.praticecollection.A@52e922
com.dev.praticecollection.A@52e922




In the above program, for every all of getRef1() method JVM encounter new keyword, JVM will create new object for class A every time and JVM will create return new object reference every time. This approach will increase no of objects in java application, it will not provide objects reusability.

In the above program. For every call of getRef2() method JVM will encounter return this statement, JVM will not create new object for class A every time. JVM will return the same reference the same reference value on which we are calling getRef2() method. This approach will increase objects reusability.















To be continue...





0 comments:

Post a Comment