Java This

Definition for java this keyword:

Java this keyword is used to refer the current instance of the method on which it is used.

Following are the ways to use java this

1) To specifically denote that the instance variable is used instead of static or local variable.That is,

private String javaFAQ;
void methodName(String javaFAQ) {
this.javaFAQ = javaFAQ;
}

Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the “this” denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective of this is used or not it denotes the instance variable.

2) Java This is used to refer the constructors

public JavaQuestions(String javapapers) {
this(javapapers, true);
}

Java This invokes the constructor of the same java class which has two parameters.

3) Java This is used to pass the current java instance as parameter

obj.itIsMe(this);

4) Similar to the above, java this can also be used to return the current instance

CurrentClassName startMethod() {
return this;
}

Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

5) Java This can be used to get the handle of the current class

Class className = this.getClass(); // this methodology is preferable in java

Though this can be done by, Class className = ABC.class; // here ABC refers to the class name and you need to know that!

As always, java this is associated with its instance and this will not work in static methods.

How is(java) this?

This Core Java tutorial was added on 29/08/2008.

«

»

Comments on "Java This"

  1. Korny says:

    What would be the difference if you used a class name instead of this?

  2. Sachindra says:

    need more and clear idea with good example

  3. Pro_hacker says:

    In short using this keyword you are calling parents of variable/method/constructor

    without it you are calling their childrens i.e.Local variables :D XD :*

  4. Pro_hacker says:

    // Perfect Example to understand “this” keyword//

    import java.io.*;

    class MeanValueClass
    {

    int x,y;
    void getdata(int x,int y)
    {

    this.x=x;
    this.y=y;

    System.out.println(“In getdata x:”+x+”\ty:”+y);

    }

    int putdata()
    {
    System.out.println(“In putdata x:”+x+”\ty:”+y);
    int ans=((x+y)/2);
    return(ans);
    }
    };

    class MeanValue
    {
    public static void main(String args[])throws IOException
    {
    int a,b;
    int z=0;
    BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
    System.out.println(“Enter any two integer numbers to find mean value”);
    a=Integer.parseInt(br.readLine());
    b=Integer.parseInt(br.readLine());

    MeanValueClass m=new MeanValueClass();
    m.getdata(a,b);

    System.out.println(“Mean value : “+m.putdata());
    }
    }

    This code will surely illustrate the use of “this”

    if you are confusing then “just remove this from getdata()”
    you will see the difference.

  5. k.manjula says:

    what is the pointer in that above question?

  6. k.manjula says:

    Explain the use of this pointer with an example program?

  7. Avik says:

    Thank you.It really helped.

  8. Amar Sannaik says:

    nicely said.

  9. rohit says:

    what is the meaning of current instance

  10. rohit says:

    what is the meaning of current instanse

  11. avinash says:

    please any body told the answers for my questions why is must compluosry this ans super in first statement

  12. avinash says:

    i want to ask one question why is must compulsory this() and super() is first statement y told the answers i m avinash sharma

  13. Zampos says:

    Thanx ,am now secured..

  14. AlegnaSamoht says:

    Thank you very much and GOD bless u Sir for ur vivid explanations.

    Would be grateful if u put some codes especially for the 2,3,4&5 ways of ‘this’ pointer.

  15. vinayak says:

    beautiful explanation

  16. geetu says:

    really very good explanation on “this” in java.. thank you s o much sir….

  17. ruchi says:

    hey…. u made things easy realy grt job.. thnx 4 it..ruchi

  18. Arjun chand says:

    Simple and good explanation,Can you please give me detail description on your point no 3 & 4.I would be greatly obliged.

  19. Anonymous says:

    this is used for return object

  20. samir says:

    I want to research about “pointer handling in Java”.. i found that it can be done by using socket programming.So,any guidance about that give me….I want to make a research about that..

  21. Jyotish says:

    5th point is not clear…sir pls explain it

  22. masterk says:

    Is “this” is a local variable of any function or it is a non-static function.

  23. madhu says:

    quite understanding

  24. ambru says:

    good sir ….

  25. vana says:

    thanks for clarifying in short

  26. Anil says:

    That about this in case of Inheritance
    like if one class extends second class

    class base
    {
    String name;
    base()
    {
    System.out.println(this.getClass()+”
    Name:-“+this.name);
    }
    base(String name)
    {
    this.name=name;
    System.out.println(this.getClass()+”

    Name:-“+this.name);
    }
    void show()
    {

    System.out.println(this.getClass()+”Name

    :Method”+this.name);
    }
    }
    class ThisExp extends base
    {
    String name;
    ThisExp(String name)
    {
    super(“Mohan”);
    this.name = name;

    System.out.println(this.getClass()+”Name

    :==”+this.name);
    }
    /* void show()
    {

    System.out.println(this.getClass()+”Name

    :Method”+this.name);
    }
    */

    public static void

    main(String s[])
    {
    base b= new ThisExp(“Amit”);
    b.show();
    }
    }
    Please Explain about this.

  27. Angel says:

    i like ur explaination
    pls explain in detail

  28. sonali says:

    Thanks for your useful information…… :D

  29. Teja says:

    Very nice.
    Can you please explain why “this” is used in
    “addkeyListener(this)” statement.What does it mean?

  30. Anonymous says:

    what is correctly mean my return this in a method

  31. Shazz says:

    Java “this” is used to invoke the constructor of the same java class which has 2 parameter.

    Thanks this was new to me

  32. Divya says:

    really helpful.. Thank you very much.

  33. Palanisamy says:

    Got some idea about THIS keyword.. Thank you very much

  34. Anonymous says:

    very well explanation…….

  35. santosh kumar chourasiya says:

    very candid explanation of “this” keyword thanks for this.

  36. Rufatet says:

    thanks for clear info

  37. mahamed says:

    thanks a lot master

  38. mahamed says:

    thanks alot

  39. ajai kumar gaur says:

    thanks a lot sir because you give me more knowledge about THIS in java.

  40. ashish bajpai says:

    IM VERY MUCH PLEASED WITH THE GIVEN INFORMATION
    CAN U PLEASE GIVE ME OUTPUT OF THE FOLLOWING LINE

    System.out.println(this);

    i know ‘this’ is used to access refrence of the current object
    rent object

  41. greenxgene says:

    I was trying hard to understand this until I read this.

  42. Palanisamy says:

    Nice Tutorial we need more..

  43. alex says:

    awesome dude!

  44. sandip says:

    to detail use the in different class how to appy to more than classes.

  45. Anonymous says:

    thankkkss yyyaarrr

  46. sudokhina says:

    plz explain 5th point with more eg..it is not so clear…it wil better if u give more eg..

  47. chandeep says:

    all clear now.
    thanks

  48. pravin says:

    thanks alot. now i am very clear about this java keyword

  49. srekanth.kolla says:

    thanks for the explanation now i am very clear about this

  50. SandeepBandari says:

    I think most of the members are not getting with 2 or 3 lines of code. We need the total explanation with program having clarity output.

  51. Anonymous says:

    so bad

  52. Bala says:

    Very clear explanation. Thanks.

  53. Alluri Reddy says:

    when we use final keyword, explain with example

  54. bhoopendra kaurav says:

    thanks for clear explan but pls give full discription over
    over point 3,4,5

  55. mukesh says:

    i m not understanding 5th points please explain it ,,,,,,,,,,,,,

  56. mukesh says:

    thanx for explain this keyword………..

  57. subbu says:

    Please explain me 3 4 5 point clearly and indepth..Please

  58. niyaz says:

    I need more idea

  59. [...] Second brace is an initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes java double brace initialization. The inner class created will have a reference to the enclosing outer class. That reference can be used using the ‘this’ pointer. [...]

  60. Anonymous says:

    gud and short explaination includes all types of using this keyword

  61. Anamika says:

    thnx for the explanation….somewhat got it……………

  62. murali says:

    tan q for given cler explanation

  63. As says:

    That was quite clear…thx

  64. tanu says:

    it was awesome,. now i understand the this keyword.

  65. surya says:

    nice but….why did’t u working for the static members
    thank u for above descrcaption….overall gooood

  66. archana chitta says:

    can u giv a detailed expln’n on this with getClass()

  67. archana chitta says:

    thanks 4 d clear explanation

  68. vikram says:

    thanks for the article, but i am still not clear at point 3,4,5. could you give more explanation keyword “this” as parameter and return type.
    thanks

  69. nandu says:

    tat was very clear.tnqs

  70. vinod says:

    Thanks… tat was quite clear… :)

Your Comment