class Banana {
void peel(int i){
//do something
}
}
public class BananaPeel {
public static void main(String[] args){
Banana a = new Banana(),
b = new Banana();
a.peel(1);
a.peel(2);
}
}
If there's only one method called peel(), how can that method know whether it's being called for the object a or b?
There's a secret first argument passed to the method peel(), and that argument is the reference to the object that's being manipulated. So the two method calls become something like:
Banana.peel(a, 1);
Banana.peel(b, 2);
Suppose you're inside a method and you'd like to get the reference to the current object. Since that reference is passed secretly by the compiler, there's no identifier for it. However, for this purpose there's a keyword: this . The this keyword - which can be used only inside a non-static mehtod - produces the reference to the object that the method has been called for. You can treat the reference just like any other object reference. Keep in mind that if you're calling a method of your class from within another method of your class, you don't need to use this. You simply call the method. The current this reference is automatically used for the other method.
public class Apricot {
void pick() {/* ... */}
void pit() {
pick(); /*...*/
}
}
Inside pit(), you could say this.pick() but there's no need to. The this keyword is used only for those special cases in which you need to explicitly use the reference to the current object.
The this keyword is also useful for passing the current object to another method.
Calling constructors from constructors
When you write several constructors for a class, there are times when you'd like to call one constructor from another to avoid duplicating code. You can make such a call by using the this keyword.
Normally, when you say this, it is in the sense of "this object" or "the current object," and by itself it produces the reference to the current object. In a constructor, the this keyword takes on a different meaning when you give it an argument list. It makes an explicit call to the constructor that matches that argument list.
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals){
petalCount = petals;
System.out.println("Constructor w/ int arg only, petalCount = " + petalCount);
}
Flower(String ss){
System.out.println("Constructor w/ Stirng arg only, s = " + ss);
s = ss;
}
Flower(String s, int petals){
this(petals);
//! this(s); //Cannot call two!
this.s = s;//Another use of "this";
System.out.println("String & int args");
}
Flower(){
this("hi", 47);
System.out.println("default constructor (no args)");
}
void PrintPetalCount(){
//! this(11);// Not inside non-constructor!
System.out.println("petalCount = " + petalCount + " s = " + s);
}
public static void main(String[] args){
Flower x = new Flower();
x.printPetalCount();
}
}
The constructor call must be the first thing you do, or you'll get a complier error message.
The meaning of static
With the this keyword in mind, you can more fully understand what is means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods (although the reverse is possible), and you can call a static method for the class itself, without any object. In fact, that's primarily what a static method is for. It's as if you're creating the equivalent of a global method. However, global methods are not permitted in Java, and putting the static method inside a class allows it access to other static methods and to static fields.