OOPS in JAVA 7
this keyword
this keyword is used to refer current class object.
this() can be used to invoke current class constructor.
this keyword can be used to invoke current class method(implicitly).
this can be passed as an argument in the method or constructor call.
this keyword can also be used to return the current class instance.
Example:
class Box
{
Double width, weight, dept;
Box (double w, double h, double d)
{
this.width = w;
this.height = h;
this.depth = d;
}
}
Here the this is used to initialize member of current object.
NOTE : Call to this() must be the first statement in constructor.
this is used to call overloaded constructor (or constructor chaining) in java.
class Car
{
private String name;
public Car()
{
this("BMW"); //oveloaded constructor is called.
}
public Car(Stting n)
{
this.name=n; //member is initialized using this.
}
}
this is also used to call Method of that class.
public void getName()
{
System.out.println("BeTheDeveloper");
}
public void display()
{
this.getName();
System.out.println();
}
this is used to return current Object.
public Car getCar()
{
return this;
}
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )