Definition for java this keyword:
Java this keyword is used to refer the current instance of the method on which it is used.
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?
Comments are closed for "Java This".
Thanks… tat was quite clear… :)
tat was very clear.tnqs
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
thanks 4 d clear explanation
can u giv a detailed expln’n on this with getClass()
nice but….why did’t u working for the static members
thank u for above descrcaption….overall gooood
it was awesome,. now i understand the this keyword.
That was quite clear…thx
tan q for given cler explanation
thnx for the explanation….somewhat got it……………
gud and short explaination includes all types of using this keyword
[…] 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. […]
I need more idea
Please explain me 3 4 5 point clearly and indepth..Please
thanx for explain this keyword………..
i m not understanding 5th points please explain it ,,,,,,,,,,,,,
thanks for clear explan but pls give full discription over
over point 3,4,5
when we use final keyword, explain with example
Very clear explanation. Thanks.
so bad
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.
thanks for the explanation now i am very clear about this
thanks alot. now i am very clear about this java keyword
all clear now.
thanks
plz explain 5th point with more eg..it is not so clear…it wil better if u give more eg..
thankkkss yyyaarrr
to detail use the in different class how to appy to more than classes.
awesome dude!
Nice Tutorial we need more..
I was trying hard to understand this until I read this.
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
thanks a lot sir because you give me more knowledge about THIS in java.
thanks alot
thanks a lot master
thanks for clear info
very candid explanation of “this” keyword thanks for this.
very well explanation…….
Got some idea about THIS keyword.. Thank you very much
really helpful.. Thank you very much.
Java “this” is used to invoke the constructor of the same java class which has 2 parameter.
Thanks this was new to me
what is correctly mean my return this in a method
Very nice.
Can you please explain why “this” is used in
“addkeyListener(this)” statement.What does it mean?
Thanks for your useful information…… :D
i like ur explaination
pls explain in detail
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.
thanks for clarifying in short
good sir ….
quite understanding
Is “this” is a local variable of any function or it is a non-static function.
5th point is not clear…sir pls explain it
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..
this is used for return object
Simple and good explanation,Can you please give me detail description on your point no 3 & 4.I would be greatly obliged.
hey…. u made things easy realy grt job.. thnx 4 it..ruchi
really very good explanation on “this” in java.. thank you s o much sir….
beautiful explanation
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.
Thanx ,am now secured..
i want to ask one question why is must compulsory this() and super() is first statement y told the answers i m avinash sharma
please any body told the answers for my questions why is must compluosry this ans super in first statement
what is the meaning of current instanse
what is the meaning of current instance
nicely said.
Thank you.It really helped.
Explain the use of this pointer with an example program?
what is the pointer in that above question?
// 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.
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 :*
need more and clear idea with good example
What would be the difference if you used a class name instead of this?
Say as detailed