Java's this keyword

Source: Internet
Author: User
Tags constructor emit expression

If you have two objects of the same type, called A and B, then you may not know how to invoke an F () method for both objects:

class Banana {void f (int i) {/* ... *}}
Banana a = n EW Banana (), B = new Banana ();
A.F (1);
B.f (2); If the

has only one method called F (), how does it know if it is called for a or B?
to write code in simple, object-oriented syntax--that is, to send messages to objects--the compiler has done some behind-the-scenes work for us. The secret is that the first argument is passed to Method F (), and that the argument is the handle of the object that is ready to operate. So the two method calls described above become the following form:

Banana.f (a,1);
BANANA.F (b,2);

This is an internal expression and we are not able to write an expression like this and try to get the compiler to accept it. But, through it, you can understand what's going on behind the scenes. The
assumes that we are inside a method and want to get the handle of the current object. Because the handle is passed by the compiler "secret", no identifiers are available. However, there is a dedicated keyword for this purpose: this. The This keyword, which is used only within the method, generates the appropriate handle for the object that has called its method. You can treat this handle like any other object handle. Note, however, that you do not have to use this if you are prepared to call a class method from within another method of one of your classes. Simply call that method. The current this handle is automatically applied to other methods. So we can use the following code:

class Apricot {
void pick () {*/* ... */}
void Pit () {pick ();/* .../}
}

Inside the pit () we can say This.pick (), but in fact there is no need. The compiler can help us do this automatically. The This keyword can only be used for those special classes-you need to explicitly use the handle of the current object. For example, if you want a handle to be returned to the current object, it is often used in a return statement.

 

: Leaf.java
//Simple with the ' this ' keyword public

class Leaf {
  private int i = 0;
  Leaf increment () {
    i++;
    return this;
  }
  void print () {
    System.out.println ("i =" + i);
  }
  public static void Main (string[] args) {
    leaf x = new Leaf ();
    X.increment (). Increment (). Increment (). print ();
  }
} ///:~


Because increment () returns the handle of the current object through the This keyword, you can easily perform multiple operations on the same object.

1. Call the builder in the builder
If you write more than one builder for a class, you often need to invoke another builder in one builder to avoid writing duplicate code. Use this keyword to do this.
Usually, when we say this, we mean "this object" or "current object." And it itself produces a handle to the current object. In a builder, if you give it a list of arguments, the This keyword has a different meaning: it makes an explicit call to the builder that matches that argument list. This allows us to invoke other builders in a direct way. As shown below:

//: Flower.java//Calling constructors with ' this ' public class Flower {private int petalcount =
  0;
  private string s = new string ("null");
    Flower (int petals) {petalcount = petals;
  System.out.println ("Constructor w/int Arg only, petalcount=" + petalcount);
    } Flower (String ss) {System.out.println ("constructor w/string Arg only, s=" + ss);
  s = SS;    } Flower (String s, int petals) {This (petals);//! This (s);
    Can ' t call two! THIS.S = s;
  Another use of the "This" System.out.println ("String & int args");
    } Flower () {This ("hi", 47);
  System.out.println ("default constructor (no args)");    } void Print () {//! This (11);
    Not inside non-constructor!
  System.out.println ("Petalcount =" + Petalcount + "s =" + s);
    public static void Main (string[] args) {Flower x = new Flower ();
  X.print (); }
} ///:~

Where the builder flower (String s,int petals) reveals to us the problem that although you can call a builder with this, you cannot call two. In addition, the builder call must be the first thing we do, otherwise we will receive an error message from the compiler.
This example also shows you another use of this. Because the name of the argument s and the name of the member data s are the same, confusion can occur. To solve this problem, you can use THIS.S to refer to member data. This form of application is often seen in Java code and is used in a large number of places in this book.
In print (), we find that the compiler does not allow us to invoke a builder from within any method other than a builder.

2. The meaning of static
Having understood the This keyword, we can understand more fully the meaning of the static (static) method. It means that a particular method does not have this. We cannot emit a call to a non-static method from within a static method (comment ②), although the converse is possible. And in the absence of any object, we can emit a call to a static method on the class itself. In fact, that's the basic meaning of the static method. It's as if we created the equivalent of a global function (in the C language). In addition to the global function not allowed in Java, if a static method is placed inside a class, it can access other static methods as well as static fields.

②: One of the situations where it is possible to emit such a call is that we pass an object handle inside the static method. Then, through the handle, which is actually this, we can call the non static method and access the non static field. But generally, if you really want to do this, just make a normal, non static method.

Some people complain that static methods are not "object-oriented" because they have certain characteristics of global functions; Using the static method, we do not have to send a message to the object because this is not present. This may be a clear argument, and if you find yourself using a lot of static methods, you should rethink your strategy. However, the concept of static is very practical, and many times it needs to be used. So as to whether they really "object oriented" should be left to theorists to discuss. In fact, even Smalltalk has something similar to static in his "class method".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.