I have a question in 'this' keyword of APEX. The following is a simple BATCH class it calls a BatchProcess class.
global class Household_BATCH implements Database.Batchable<SObject>, Database.Stateful {
global Id batchJobId;
private Process_Service ProcessService;
global Household_BATCH() {
this.ProcessService = new Process_Service();
}
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, AccountId, Account.RecordType.Name FROM Contact WHERE Account.RecordType.Name = \'Household\' AND Id IN (SELECT Contact__c FROM Request__c WHERE Status__c = \'Processing\')';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Contact> contacts) {
try {
this.batchJobId = bc.getJobId();
this.ProcessService.updateHouseholdAccountsContactsAccount(contacts, this.batchJobId);
} catch (Exception e) {
}
}
global void finish(Database.BatchableContext bc) {
}
}
In the above BATCH I defined the batchJobId, ProcessService as global variable and assign value or access this value inside methods like in the class. Here my question is, Is it necessary to put 'this' keyword when you access the global variable? I am really confused here because everything is worked as I expect even I used the 'this' keyword or not.