Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
The word 'this'
October 16, 2010 09:46PM
When a constructor is written with the specific fields, when are we suppose to use the word 'this' in front of the fields?
Like the example below...

class Pet {

public static String type, breed;
public static int age;

Pet() {
this.type = "unknown";
this.breed = "unknown";
this.age = 0;

Pet(String ftype, String fbreed, int fage) {

this.type = ftype;
this.breed = fbreed;
this.age = fage;

When must the word 'this' be used? Everytime when a default or constructor with paramaters are written? If the word 'this' is left out will it make any difference?
Re: The word 'this'
October 18, 2010 07:04AM
See here:

Language: Java
Using the this Keyword Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. Using this with a Field The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. For example, the Point class was written like this   public class Point { public int x = 0; public int y = 0;   //constructor public Point(int a, int b) { x = a; y = b; } }   but it could have been written like this: public class Point { public int x = 0; public int y = 0;   //constructor public Point(int x, int y) { this.x = x; this.y = y; } }   Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x. Using this with a Constructor From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here's another Rectangle class, with a different implementation from the one in the Objects section. public class Rectangle { private int x, y; private int width, height;   public Rectangle() { this(0, 0, 0, 0); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } ... }   This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments. If present, the invocation of another constructor must be the first line in the constructor.
Sorry, only registered users may post in this forum.

Click here to login