The this keyword in Java:
thisis a reserved keyword that translates to "this"thisis a reference variable that holds the memory address of the current object instance- Each object has its own
thisreference; creating multiple objects results in distinctthisinstances thisresides in the heap memory within the Java objectthiscan be used inside instance methods where it refers to the calling objectthiscannot be used in static methods
Test 1:
public class Client {
// Name attribute
String name;
// Constructor
public Client(){
}
// Shopping behavior
// Not marked with static
// Instance variables and methods require object references for invocation
// Since each client shops independently, this should be an instance method
// Note: When an action involves object participation, use instance methods (non-static)
public void purchase(){
System.out.println(this.name + " is shopping");
// this refers to the object executing this instance method
}
public static void performAction(){
// System.out.println(name); Error
// No "current object" exists because static methods are called via Class.method
// name refers to the instance variable of the current object
// System.out.println(this); Error
// Static methods don't need an object instance; they're invoked via class name
// So there's no current object during execution
}
}
public class ThisDemo01{
public static void main(String[] args) {
// Instantiate Client objects
Client c1 = new Client();
c1.name = "jock";
// Instantiate another Client object
Client c2 = new Client();
c2.name = "rose";
// Perform shopping actions
c1.purchase();
c2.purchase();
// Call static method
Client.performAction();
}
}
Test 2:
public class ThisDemo02 {
// Instance variable (reference)
int value = 10;
public static void main(String[] args){
// System.out.println(value); Error
// Neither this nor instance variables can be accessed in static methods
// System.out.println(this.value); Error
// Same reason as above
}
}
Test 3:
public class ThisDemo03 {
public static void main(String[] args) {
// Invoke doSome method
ThisDemo03.doSome();
// Or
doSome();
// Invoke doOther method
// ThisDemo03.doOther() Error
// Instance methods require object references, but we are inside the class itself
// this.doOther(); Error
// Because main is a static method, there's no implicit this
}
// Static method
public static void doSome() {
System.out.println("do some!");
}
// Instance method
public void doOther() {
System.out.println("do other!");
}
// Instance method
public void execute() {
// To invoke execute, an object must exist
System.out.println("execute !");
doOther(); // Successful call
// Since execute is running, the object exists, so doOther can be called
this.doOther(); // Same principle
}
}
Conclusion:
- Static methods cannot directly access instance variables and methods
- This is because instence members require an object to exist
- Static methods have no
this, impyling no currant object exists - Hence, they cannot access the current object's instance variables and methods
When this cannot be omitted:
- When distinguishing between local variables and instance variables,
thismust be used
public class Account {
private int accountId;
private String accountName;
/*
public void setAccountId(int a) {
accountId = a;
}
*/
// Parameter name differs from accountId, so no confusion
// accountId is the instance variable, a is the parameter
/*
public void setAccountId(int accountId) {
accountId = accountId;
}
*/
// Parameter name matches accountId, due to scope rules,
// the first accountId refers to the parameter, second to the instance variable
// To correctly assign parameter to instance variable, use
public void setAccountId(int accountId) {
this.accountId = accountId;
}
}
Where can this be used?
- Inside instance methods, representing the current object
- Syntax:
this. - In constructors, to call other constructors within the same class
- Syntax:
this(arguments);
Important: this() must appear as the first statement in a constructor
Object-Oriented Programming in Java
- Introduction to OOP in Java
- Distinguishing classes and objects
- Encapsulation in Java
- Constructors in Java
- The
thiskeyword in Java - The
statickeyword in Java - Inheritance in Java
- The
finalkeyword in Java - Package and import statements