This (computer science)
Encyclopedia
In many object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s, this (also called self or Me) is a keyword
Keyword (computer programming)
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....

 that is used in instance methods to refer to the object on which they are working. C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 and languages which derive in style from it (such as Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

, C#, and PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

) generally use this. Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...

 and others such as Object Pascal
Object Pascal
Object Pascal refers to a branch of object-oriented derivatives of Pascal, mostly known as the primary programming language of Embarcadero Delphi.-Early history at Apple:...

, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, and Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

 use self; Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...

 uses Me.

The concept is the same in all languages. For brevity, here we just say this.

this is usually an immutable reference
Reference (computer science)
In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

 or pointer which refers to the current object. Some languages, such as Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

, allow assignment to this, although it is deprecated. Doing so can be very misleading to maintenance programmers, because the assignment does not modify the original object, only changing which object that the rest of the code in the method refers to, and can end with undefined behavior.

After an object is properly constructed this is always a valid reference. Some languages require it explicitly; others use lexical scoping to use it implicitly to bring symbols within their class visible. In the latter case, the use of this in code, while not illegal, may raise warning bells to a maintenance programmer, although there are still legitimate uses of this in this case, such as referring to instance variables hidden by local variables of the same name, or if the method wants to return a reference to the current object, i.e. this, itself.

this becomes an extra parameter to an instance method. For example, the following instance method in C++
int foo::print (bar x)
is essentially equivalent to the procedural programming:
int foo_print (foo *const this, bar x)

In some languages, for example Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 and Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 5, this is made explicit, the first parameter of an instance method being such a reference. It needs explicitly to be specified. In this case, the parameter need not necessarily be named this or self; like any other parameter, it can be freely named by the programmer; however, by informal convention, the first parameter of an instance method in Perl and Python is named self.

In some compilers (for example GCC
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

), pointers to C++ instance methods can be directly cast to a pointer of another type, with an explicit this pointer parameter.

Static methods in C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 or Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 are not associated with instances but classes, and so cannot use this, because there is no object. In others, such as Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, Smalltalk
Smalltalk
Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...

 or Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

, the method is associated with a class object that is passed as this, and are called class methods.

C++

Early versions of C++ would let the this pointer be changed; by doing so a programmer could change which object a method was working on. This feature was eventually removed, and now this in C++ is an rvalue
.

Early versions of C++ did not include references and it has been suggested that had they been so in C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 from the beginning, this would have been a reference, not a pointer. But on the other hand, others say that this being a pointer makes life simpler because it avoids complications that could arise from the class having its address-of
Memory address
A digital computer's memory, more specifically main memory, consists of many memory locations, each having a memory address, a number, analogous to a street address, at which computer programs store and retrieve, machine code or data. Most application programs do not directly read and write to...

 operator overloaded.

C++ lets objects destroy themselves with the idiom delete this.

Java

A Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods.

Since all instance methods are virtual in Java, this can never be null.

"...the reason for using this construct [this] is that we have a situation that is known as name overloading - the same name being used for two different entities....It is important to understand that the fields and the parameters are separate variables that exist independently of each other, even though they share similar names. A parameter and a field sharing a name is not really a problem in Java." - Objects First with Java, D. Barnes and M. Kölling

C#

this in C# works the same way as in Java, for reference types. However, within C# "value types", this has quite different semantics, being similar to an ordinary mutable variable reference, and can even occur on the left side of an assignment.

Dylan

In Dylan, an OO language that supports multimethods and hasn't a concept of this, sending a message to an object is still kept in the syntax. The two forms below work the same; the differences are just syntactic sugar
Syntactic sugar
Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express....

.
object.method(param1, param2)

and

method (object, param1, param2)

Python

In Python, there is no keyword for this, but it exists as the name of the obligatory first argument of all member functions, to which the object instance it is called on is automatically bound. Conventionally, the name self is used. In class methods (created with the classmethod decorator), the first argument refers to the class object itself. In static methods (created with the staticmethod decorator), no special first argument exists.

Xbase++

Self is strictly used within methods of a class.
Another way to refer to Self is to use ::.

External links

  • The Design and Evolution of C++ by Bjarne Stroustrup
    Bjarne Stroustrup
    Bjarne Stroustrup ; born December 30, 1950 in Århus, Denmark) is a Danish computer scientist, most notable for the creation and the development of the widely used C++ programming language...

     - Addison-Wesley Pub Co; 1st edition (March 29, 1994); ISBN 0-201-54330-3
  • More Effective C++: 35 New Ways to Improve Your Programs and Designs by Scott Meyers
    Scott Meyers
    Scott Douglas Meyers is an American author and software consultant, specializing in the C++ computer programming language. He is known for his Effective C++ book series. He is a frequent speaker at conferences and trade shows. He holds a Ph.D. in computer science from Brown University and M.S...

    -- (1995) ISBN 0-201-63371-X
  • Java this
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK