01
Oct
Black Art of Java Game Programming by Joel
Black Art of Java Game Programming by Joel Fan Sams, Macmillan Computer Publishing ISBN: 1571690433 Pub Date: 11/01/96 Previous Table of Contents Next The keyword this is necessary in the constructor for DancingRect to distinguish the local variables (which are temporary) from instance variables of the same name (which provide storage for the object). Here s another example of this, taken from the Broadway animation applet above. animation = new Thread(this); The Thread constructor takes an object as its argument. By passing this, the Broadway applet object passes a reference to itself as the argument. Using this in Constructors There s one more way that this is used. Methods, and constructors in particular, can be overloaded. In other words, a class can have several constructors, as long as each one has a distinct signature (i.e., argument list). When this, followed by an argument list, is the first statement in a constructor, the appropriate constructor is called. For example, look at the following class, which has two constructors that invoke each other: class A { // constructor #1 public A(int x, float y) { this(y); // invoke constructor #2 … } // constructor #2 public A(float x) { this((int)x, x); // invoke constructor #1 … } } As you see, the this statement must occur at the beginning of each constructor. this() (this with no arguments) calls the constructor with no arguments.
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra servlet hosting services
