Sie sind auf Seite 1von 3

Java Tutorial: this Keyword

In Java there's this keyword. It can be used inside methods (and constructors). The this keyword is used like a constant. It's value is the reference to the current object. For example, if you have a class named CL, and it has method named me, then this in it would be a reference to a instance of the CL (that is: a CL object).

Example 1
class CL { int x = 1; CL me () {return this;} } public class Thiss { public static void main(String[] args) { CL cl = new CL(); System.out.println( cl.x ); System.out.println( cl.me().x ); // same as above System.out.println( cl.me().me().x ); // same as above } }

In the above example, the method me returns this. So, cl.me() is equivalent to the object cl itself. Therefore, cl.x and cl.me().x and cl.me().me().x are all the same.

Example 2
class OneNumber { int n; void setValue (int n) {this.n=n;}; } public class Thatt { public static void main(String[] args) { OneNumber x = new OneNumber(); x.setValue(3); System.out.println( x.n ); } }

In the above example, the method setValue tries to set the value of its argument to the class variable n. Because the name n is already used in the parameter name, so n=n is absurd. The workaround is to use the this keyword to refer to the object. Thus we have this.n=n.

Example 3

Another practical example of using this is when you need to pass your current object to another method. Example:
class B { int n; void setMe (int m) { C h = new C(); h.setValue(this, m); }; } class C { void setValue (B obj, int h) {obj.n=h;}; } public class A { public static void main(String[] args) { B x = new B(); x.setMe(3); System.out.println( x.n ); } }

In the above example, B has a member variable n. It has a method setMe. This method calls another class method and passing itself as a object. There is also a super keyword used to refer to the parent class. See super keyword. There are certain coding techniques that require this in Java, beyond cosmetic preference. Here are two examples that I use fairly often. First, if you want to use method chaining within a class so that the method returns the same type as the type it belongs to, you should use this.'
public class MyBuilder private long id; private String name; {

public MyBuilder id(long id) { this.id = id; return this; } public MyBuilder name(String name) { this.name = name; return this; } public AnotherType build() { // business logic that consumes instance variables to build AnotherType } }

The other case is the Visitor pattern


public interface Visitor<V extends Visitable> { public void visit(V visitable); } public interface Vistable<V extends Visitor> { public void accept(V visitor); }

And its implementations will say:


public class ConcreteVisitable implements Visitable<ConcreteVisitor> { public void accept(ConcreteVisitor visitor) { // grant access to ConcreteVisitable's state to the Visitor visitor.visit(this); } } public class ConcreteVisitor implements Visitor<ConcreteVisitable> { public void visit(ConcreteVisitable target) { // business logic that operates on the state of ConcreteVisitable } }

Notice that ConcreteVisitable uses this to allow its visitor's access to its state. I'm using generics on the interfaces so that I can enforce a 1-1 mapping between ConcreteVisitors and ConcreteVisitables and so that ConcreteVisitors have particular knowledge of their targets without needing a class cast..

Das könnte Ihnen auch gefallen