Tuesday, 30 August 2016

this keywords

“this” keyword :-
“this”  is a java keyword, it able to represent current class object.
In java applications, we are able to utilize “this” keyword in the following four ways.
1.       To refer current class variable
2.       To refer current class method
3.       To refer current class constructor
4.       To refer current class Object 

1.       To refer current class variable
In java applications, if we want to refer current class variables by using “this” keyword then we have to use the following
Syntax:-
this.varName

Note:-
In java applications, when we have requirements to refer class level variable over the local variable in the case of having same name at local variables and class level variables then we have to use “this” keyword.
Ex code:-
class A
{
                int i=10;
                int j=20;
                A(int i, int j)
                {
                                System.out.println(i+" "+j);
                                System.out.println(this.i+" "+this.j);
                }             
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A(30,40);
                }
}
Output:-
30 40
10 20
In java bean classes, we will use setXXX(-) method to set data to the respective variables in java bean object, in java bean classes, we will provide same names to the local variable and class level variables and we will assign local variable data to class level variables, in this context, we have to refer class level variables separately over local variables, for this, we have to use “this” keyword.
Ex code:-  
class User
{
                private String uname;
                private String upwd;
                public void setUname(String uname)
                {
                                this.uname=uname;
                }
                public void setUpwd(String upwd)
                {
                                this.upwd=upwd;
                }
                public String getUname()
                {
                                return uname;
                }
                public String getUpwd()
                {
                                return upwd;
                }
}
class Test
{
                public static void main(String[] args)
                {
                                User u=new User();
                                u.setUname("abc");
                                u.setUpwd("abc123");
                                System.out.println("User Authentication Details");
                                System.out.println("----------------------------");
                                System.out.println("User Name    :"+u.getUname());
                                System.out.println("Password     :"+u.getUpwd());
                }
}
Output:-
User Authentication Details
----------------------------
User Name    :abc
Password     :abc123

2.       To refer current class method:
If we want to refer current class method by using “this” keyword then we have to use the following syntax.

this.method_Name([Param_List]);

Note :-
In java applications, to access current class method no need to use any reference variable and any keyword, directly we can access current class methods.
Ex code :-
class A
{
                void m1()
                {
                                System.out.println("m1-A");
                                m2();
                                this.m2();
                }
                void m2()
                {
                                System.out.println("m2-A");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                                a.m1();
                }
}
Output:-
m1-A
m2-A
m2-A

3.       To refer current class constructor:
If we want to refer current class constructor, using “this” keyword then we have to use the following   Syntax.

this([Param_List]);
Ex code :-
class A
{
                A()
                {
                                this(10);
                                System.out.println("A-Con");
    }
    A(int i)
    {
                this(22.22f);
                System.out.println("A-int-Param-Con");
    }
    A(float f)
    {
                this(33.33);
                System.out.println("A-float-Param-Con");
    }
    A(double d)
    {
                System.out.println("A-double-Param-Con");
    }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                }
}
Output:-
A-double-Param-Con
A-float-Param-Con
A-int-Param-Con
A-Con

If we want to access current class constructor by using “this” keyword then the respective “this” statement must be provided as “first” statements .
Ex code :-
class A
{
                A()
                {
                                System.out.println("A-Con");
                                this(10);
    }
    A(int i)
    {
                System.out.println("A-int-Param-Con");
                this(22.22f);
    }
    A(float f)
    {
                System.out.println("A-float-Param-Con");
                this(33.33);
    }
    A(double d)
    {
                System.out.println("A-double-Param-Con");
    }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                }
}
Status:- compilation error, call to this must be first statements constructor.

In java applications, if we want to access current class constructor by using “this” keyword then the respective “this” statements must be provided in the current class another constructor, it must not be provide in normal java methods.
Ex code:-
class A
{
                A()
                {
                                System.out.println("A-Con");
                }
                A(int i)
                {
                                System.out.println("A-int-Param-Con");
                }
                void m1()
                {
                                this(10);
                                System.out.println("m1-A");
                }
               
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                                a.m1();
                }
}
Status: Compilation Error, call to this must be first statement constructor.
Note :-
If we access all the current class constructor by using “this” keyword in a chain faction then process is called as “Constructor chaining”.

Date july-30-2016

4.       To return current class Objects:-
If we want to return class Object by using “this” keyword then we have to use the following Syntax.

return this;
Ex code:-
class A
{
                A getRef1()
                {
                                A a=new A();
                                return a;
                }
                A getRef2()
                {
                                return this;
                }
}
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(a.getRef1());
                                System.out.println();
                                System.out.println(a.getRef2());
                                System.out.println(a.getRef2());
                                System.out.println(a.getRef2());
                }
}
Output:-
A@1908cal

A@100ab23
A@3b895
A@6b7920

A@1908cal
A@1908cal
A@1908cal

In the above application, if we call getRef1() method multiple time then JVM will encounter “new” keyword every time, where JVM will create new object for class A every time and JVM will return new object reference every time, it will increase number of duplicate objects in java applications, it is not suggestible.
In the above application, If we call getRef2() method multiple times JVM encounter “this” keyword every time, JVM will return the same reference value on which we accessed getRef2() method, this approach will increase Object Reusability.


No comments:

Post a Comment