faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 24 people (63%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

Java: How to use the 'this' command in Java?

May 25th, 2006 19:40
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 24 April 2001 - 03:29 pm ----------------------

Java: How to use the 'this' command in Java?

---

Using the 'this' keyword

---

You can use a special read-only variable, called 'this', to explicitly
refer to your object.

---

A method can use 'this' to refer to the instance of the object that 
holds
it.

---

For example to tell the compiler which variable names correspond to
class members and which variable names correspond to method parameters,
precede the class member variable names with the 'this' keyword, as
shown here:

---

--- cut here: begin --------------------------------------------------

// save as ConstructorOverload.java [kn, ri, su, 28-04-2002 17:57:05]
import java.awt.*;
import java.applet.*;
//
class CLASSEmployee {
 public String nameS;
 public int employeeidI;
 public int officenumberI;
 public double salaryD;
 //
 public static int defaultidI = 300;
 public static int defaultofficeI = 200;
 public static double defaultsalaryD = 10000.0;
 //
 public void PROCEmployeeShowRecord( Graphics g, int xI, int yI ) {
  g.drawString( "Name = " + nameS, xI, yI + 0 * 20 );
  g.drawString( "Office = " + officenumberI, xI, yI + 1 * 20 );
  g.drawString( "Employee Id = " + employeeidI, xI, yI + 2 * 20 );
  g.drawString( "Salary = $" + salaryD, xI, yI + 3 * 20 );
 }
 // constructor overload
 public CLASSEmployee( String nameS, int employeeidI, int 
officenumberI, double salaryD ) {
  this.nameS = nameS;
  this.employeeidI = employeeidI;
  this.officenumberI = officenumberI;
  this.salaryD = salaryD;
 }
 // constructor overload
 public CLASSEmployee( String nameS ) {
  this.nameS = nameS;
  employeeidI = defaultidI++;
  officenumberI = defaultofficeI++;
  salaryD = defaultsalaryD;
 }
}
//
public class ConstructorOverload extends Applet {
 //
 public void paint( Graphics g ) {
  CLASSEmployee bossO = new CLASSEmployee( "Suzanne" );
  CLASSEmployee topworkerO = new CLASSEmployee( "Vanessa", 101, 124, 
25000.0 );
  CLASSEmployee worker1O = new CLASSEmployee( "Nathalie" );
  //
  bossO.PROCEmployeeShowRecord( g, 5, 20 );
  topworkerO.PROCEmployeeShowRecord( g, 5, 120 );
  worker1O.PROCEmployeeShowRecord( g, 5, 220 );
 }
}

---

<!-- save as ConstructorOverload.htm [kn, ri, su, 28-04-2002 
17:56:58] -->
<HTML>
 <TITLE>
   ConstructorOverload Applet
 </TITLE>
  <APPLET
    CODE = "ConstructorOverload.class"
    WIDTH = "400"
    HEIGHT = "300"
  >
  </APPLET>
</HTML>

---

// when run you will see the following:
//
// Name = Suzanne
// Office = 200
// Employee Id = 300
// Salary = $10000.0
//
// Name = Vanessa
// Office = 124
// Employee Id = 101
// Salary = $25000.0
//
// Name = Nathalie
// Office = 201
// Employee Id = 301
// Salary = $10000.0

--- cut here: end ----------------------------------------------------

===

[book: source: Jamsa, Kris - Java Now! - ISBN 1-884133-30-4 - p. 
106 'Using the 'this' keyword' - 
http://www.amazon.com/exec/obidos/tg/detail/-
/1884133304/qid=1064843888/sr=1-3/ref=sr_1_3/103-6292513-4946208?
v=glance&s=books]

===

Internet: see also:

---

C#: How to use the 'this' command in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24776/fid/791

---

C++: How to use the 'this' command in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/16106

---

Delphi: How to use the 'this' command (='self') in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/16108/fid/175

---

Visual Basic: How to use the 'this' command (='Me') in Visual 
Basic.NET?
http://www.faqts.com/knowledge_base/view.phtml/aid/27372/fid/1610

----------------------------------------------------------------------