Adding a Wrapper Class to an External Class
I am confused about what is happening in a small section of some code. I have a class that has a wrapper class inside it. There is an external class that calls this wrapper class Models.add(new CheckItemsClass(...))
The part that confuses me is that the Models.cls
does not have any reference or instance of CheckItemsClass
. So how are we able to add a new CheckItemsClass
to the Models.cls
?
public class ChecklistController{
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
public class CheckItemClass{
public string item {get;set;}
public string notes {get;set;}
public boolean flag {get;set;}
public CheckItemClass(string i,string n,boolean f){
this.item = i;
this.notes = n;
this.flag = f;
}
}
}
public without sharing class Models {
public static void mdlEncinitas(List<WC_Credit_Decision__c> wcObs){...}
public static void mdlDataV1(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlDataV2(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlEncinitasRecommender(WC_Credit_Decision__c wcOb, Integer score10pt, Integer score, String productionChannel, Double grossSalesRunRate, Double judgmentTotalAmt, Integer maxTib, Double avgOfAvgDailyBal, Double bkDateYrs, Integer maxFICO, String sicCode, NF_SIC_4__c sicOb, Credit_Review__c crOb, WC_Buy_Rate__c brOb, Boolean lenderMissing, Double maxNegDaysLst3Mnths, String oppType, List<Decision_Attribute__c> decisionAttr, Double maxNegDaysEver, Double maxNSFsEver) {...}
public static void mdlEncinitasRun(ID wcId, WC_Credit_Decision__c wcOb, Credit_Review__c crOb, Opportunity oppOb, List<Monthly_Review__c> mrObs,NF_SIC_4__c sicOb, Account acctOb, WC_Buy_Rate__c brOb, Boolean brokerMissing, Map<Id,List<Credit_Review_Monthly_Ledger__c>> CR_ledgers_map, List<Decision_Attribute__c> decisionAttr){...}
}
apex class wrapper-class inner-class
add a comment |
I am confused about what is happening in a small section of some code. I have a class that has a wrapper class inside it. There is an external class that calls this wrapper class Models.add(new CheckItemsClass(...))
The part that confuses me is that the Models.cls
does not have any reference or instance of CheckItemsClass
. So how are we able to add a new CheckItemsClass
to the Models.cls
?
public class ChecklistController{
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
public class CheckItemClass{
public string item {get;set;}
public string notes {get;set;}
public boolean flag {get;set;}
public CheckItemClass(string i,string n,boolean f){
this.item = i;
this.notes = n;
this.flag = f;
}
}
}
public without sharing class Models {
public static void mdlEncinitas(List<WC_Credit_Decision__c> wcObs){...}
public static void mdlDataV1(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlDataV2(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlEncinitasRecommender(WC_Credit_Decision__c wcOb, Integer score10pt, Integer score, String productionChannel, Double grossSalesRunRate, Double judgmentTotalAmt, Integer maxTib, Double avgOfAvgDailyBal, Double bkDateYrs, Integer maxFICO, String sicCode, NF_SIC_4__c sicOb, Credit_Review__c crOb, WC_Buy_Rate__c brOb, Boolean lenderMissing, Double maxNegDaysLst3Mnths, String oppType, List<Decision_Attribute__c> decisionAttr, Double maxNegDaysEver, Double maxNSFsEver) {...}
public static void mdlEncinitasRun(ID wcId, WC_Credit_Decision__c wcOb, Credit_Review__c crOb, Opportunity oppOb, List<Monthly_Review__c> mrObs,NF_SIC_4__c sicOb, Account acctOb, WC_Buy_Rate__c brOb, Boolean brokerMissing, Map<Id,List<Credit_Review_Monthly_Ledger__c>> CR_ledgers_map, List<Decision_Attribute__c> decisionAttr){...}
}
apex class wrapper-class inner-class
add a comment |
I am confused about what is happening in a small section of some code. I have a class that has a wrapper class inside it. There is an external class that calls this wrapper class Models.add(new CheckItemsClass(...))
The part that confuses me is that the Models.cls
does not have any reference or instance of CheckItemsClass
. So how are we able to add a new CheckItemsClass
to the Models.cls
?
public class ChecklistController{
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
public class CheckItemClass{
public string item {get;set;}
public string notes {get;set;}
public boolean flag {get;set;}
public CheckItemClass(string i,string n,boolean f){
this.item = i;
this.notes = n;
this.flag = f;
}
}
}
public without sharing class Models {
public static void mdlEncinitas(List<WC_Credit_Decision__c> wcObs){...}
public static void mdlDataV1(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlDataV2(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlEncinitasRecommender(WC_Credit_Decision__c wcOb, Integer score10pt, Integer score, String productionChannel, Double grossSalesRunRate, Double judgmentTotalAmt, Integer maxTib, Double avgOfAvgDailyBal, Double bkDateYrs, Integer maxFICO, String sicCode, NF_SIC_4__c sicOb, Credit_Review__c crOb, WC_Buy_Rate__c brOb, Boolean lenderMissing, Double maxNegDaysLst3Mnths, String oppType, List<Decision_Attribute__c> decisionAttr, Double maxNegDaysEver, Double maxNSFsEver) {...}
public static void mdlEncinitasRun(ID wcId, WC_Credit_Decision__c wcOb, Credit_Review__c crOb, Opportunity oppOb, List<Monthly_Review__c> mrObs,NF_SIC_4__c sicOb, Account acctOb, WC_Buy_Rate__c brOb, Boolean brokerMissing, Map<Id,List<Credit_Review_Monthly_Ledger__c>> CR_ledgers_map, List<Decision_Attribute__c> decisionAttr){...}
}
apex class wrapper-class inner-class
I am confused about what is happening in a small section of some code. I have a class that has a wrapper class inside it. There is an external class that calls this wrapper class Models.add(new CheckItemsClass(...))
The part that confuses me is that the Models.cls
does not have any reference or instance of CheckItemsClass
. So how are we able to add a new CheckItemsClass
to the Models.cls
?
public class ChecklistController{
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
public class CheckItemClass{
public string item {get;set;}
public string notes {get;set;}
public boolean flag {get;set;}
public CheckItemClass(string i,string n,boolean f){
this.item = i;
this.notes = n;
this.flag = f;
}
}
}
public without sharing class Models {
public static void mdlEncinitas(List<WC_Credit_Decision__c> wcObs){...}
public static void mdlDataV1(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlDataV2(List<WC_Credit_Decision__c> wcObs, List<ID> wccdIds) {...}
public static void mdlEncinitasRecommender(WC_Credit_Decision__c wcOb, Integer score10pt, Integer score, String productionChannel, Double grossSalesRunRate, Double judgmentTotalAmt, Integer maxTib, Double avgOfAvgDailyBal, Double bkDateYrs, Integer maxFICO, String sicCode, NF_SIC_4__c sicOb, Credit_Review__c crOb, WC_Buy_Rate__c brOb, Boolean lenderMissing, Double maxNegDaysLst3Mnths, String oppType, List<Decision_Attribute__c> decisionAttr, Double maxNegDaysEver, Double maxNSFsEver) {...}
public static void mdlEncinitasRun(ID wcId, WC_Credit_Decision__c wcOb, Credit_Review__c crOb, Opportunity oppOb, List<Monthly_Review__c> mrObs,NF_SIC_4__c sicOb, Account acctOb, WC_Buy_Rate__c brOb, Boolean brokerMissing, Map<Id,List<Credit_Review_Monthly_Ledger__c>> CR_ledgers_map, List<Decision_Attribute__c> decisionAttr){...}
}
apex class wrapper-class inner-class
apex class wrapper-class inner-class
asked Dec 14 '18 at 20:59
OliviaOlivia
1,321423
1,321423
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Somewhere else in the local context, there is a variable declaration that's shadowing the class Models
.
Example:
public class TestQ242685 {
public class InnerClass {
}
public static void runTest() {
List<InnerClass> SomeOtherClass = new List<InnerClass>();
SomeOtherClass.add(new InnerClass());
System.debug(SomeOtherClass);
}
}
public class SomeOtherClass {
// No `add()` method!
}
Call TestQ242685.runTest()
and sure enough, you'll see
16:04:08:034 USER_DEBUG [10]|DEBUG|(InnerClass:)
You've got a List<CheckItemClass> Models
declared somewhere.
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
add a comment |
You seem to have a name shadowing here.
This code is adding CheckItemClass
objects in a List<CheckItemClass>
which is most likely declared in your controller as:
List<CheckItemClass> Models = new List<CheckItemClass>();`
And from documentation:
In particular it is legal for a variable, method, and a class within a class to have the same name.
So that's why you are not getting any compile errors with same names. It's always a good idea to keep names specific to the context.
And it's the same variable that's being used in your fillItems()
method as below:
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
So Models.add()
is not really calling any method in the Models
class, but the List.add()
method. Also refer to the return statement which confirms as what you are returning.
thank you! I realized I missed theList<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!
– Olivia
Dec 14 '18 at 21:11
add a comment |
Name shadowing can take many forms...
Here is my personal favorite (as I have made this mistake more than once wrto name shadowing) from the lazy loading pattern...
public Contact contacts {
get {
if (contacts == null) {
Contact contacts = [SELECT ... FROM Contact WHERE ...]; //oops - declared new vbl!
}
return contacts;
} private set;
}
if (contacts.isEmpty()) {...} . // null pointer exception!
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242685%2fadding-a-wrapper-class-to-an-external-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Somewhere else in the local context, there is a variable declaration that's shadowing the class Models
.
Example:
public class TestQ242685 {
public class InnerClass {
}
public static void runTest() {
List<InnerClass> SomeOtherClass = new List<InnerClass>();
SomeOtherClass.add(new InnerClass());
System.debug(SomeOtherClass);
}
}
public class SomeOtherClass {
// No `add()` method!
}
Call TestQ242685.runTest()
and sure enough, you'll see
16:04:08:034 USER_DEBUG [10]|DEBUG|(InnerClass:)
You've got a List<CheckItemClass> Models
declared somewhere.
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
add a comment |
Somewhere else in the local context, there is a variable declaration that's shadowing the class Models
.
Example:
public class TestQ242685 {
public class InnerClass {
}
public static void runTest() {
List<InnerClass> SomeOtherClass = new List<InnerClass>();
SomeOtherClass.add(new InnerClass());
System.debug(SomeOtherClass);
}
}
public class SomeOtherClass {
// No `add()` method!
}
Call TestQ242685.runTest()
and sure enough, you'll see
16:04:08:034 USER_DEBUG [10]|DEBUG|(InnerClass:)
You've got a List<CheckItemClass> Models
declared somewhere.
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
add a comment |
Somewhere else in the local context, there is a variable declaration that's shadowing the class Models
.
Example:
public class TestQ242685 {
public class InnerClass {
}
public static void runTest() {
List<InnerClass> SomeOtherClass = new List<InnerClass>();
SomeOtherClass.add(new InnerClass());
System.debug(SomeOtherClass);
}
}
public class SomeOtherClass {
// No `add()` method!
}
Call TestQ242685.runTest()
and sure enough, you'll see
16:04:08:034 USER_DEBUG [10]|DEBUG|(InnerClass:)
You've got a List<CheckItemClass> Models
declared somewhere.
Somewhere else in the local context, there is a variable declaration that's shadowing the class Models
.
Example:
public class TestQ242685 {
public class InnerClass {
}
public static void runTest() {
List<InnerClass> SomeOtherClass = new List<InnerClass>();
SomeOtherClass.add(new InnerClass());
System.debug(SomeOtherClass);
}
}
public class SomeOtherClass {
// No `add()` method!
}
Call TestQ242685.runTest()
and sure enough, you'll see
16:04:08:034 USER_DEBUG [10]|DEBUG|(InnerClass:)
You've got a List<CheckItemClass> Models
declared somewhere.
answered Dec 14 '18 at 21:05
David ReedDavid Reed
36.3k72254
36.3k72254
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
add a comment |
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
ah you are right, it was at the very top. I totally missed it. thank you!
– Olivia
Dec 14 '18 at 21:11
add a comment |
You seem to have a name shadowing here.
This code is adding CheckItemClass
objects in a List<CheckItemClass>
which is most likely declared in your controller as:
List<CheckItemClass> Models = new List<CheckItemClass>();`
And from documentation:
In particular it is legal for a variable, method, and a class within a class to have the same name.
So that's why you are not getting any compile errors with same names. It's always a good idea to keep names specific to the context.
And it's the same variable that's being used in your fillItems()
method as below:
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
So Models.add()
is not really calling any method in the Models
class, but the List.add()
method. Also refer to the return statement which confirms as what you are returning.
thank you! I realized I missed theList<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!
– Olivia
Dec 14 '18 at 21:11
add a comment |
You seem to have a name shadowing here.
This code is adding CheckItemClass
objects in a List<CheckItemClass>
which is most likely declared in your controller as:
List<CheckItemClass> Models = new List<CheckItemClass>();`
And from documentation:
In particular it is legal for a variable, method, and a class within a class to have the same name.
So that's why you are not getting any compile errors with same names. It's always a good idea to keep names specific to the context.
And it's the same variable that's being used in your fillItems()
method as below:
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
So Models.add()
is not really calling any method in the Models
class, but the List.add()
method. Also refer to the return statement which confirms as what you are returning.
thank you! I realized I missed theList<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!
– Olivia
Dec 14 '18 at 21:11
add a comment |
You seem to have a name shadowing here.
This code is adding CheckItemClass
objects in a List<CheckItemClass>
which is most likely declared in your controller as:
List<CheckItemClass> Models = new List<CheckItemClass>();`
And from documentation:
In particular it is legal for a variable, method, and a class within a class to have the same name.
So that's why you are not getting any compile errors with same names. It's always a good idea to keep names specific to the context.
And it's the same variable that's being used in your fillItems()
method as below:
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
So Models.add()
is not really calling any method in the Models
class, but the List.add()
method. Also refer to the return statement which confirms as what you are returning.
You seem to have a name shadowing here.
This code is adding CheckItemClass
objects in a List<CheckItemClass>
which is most likely declared in your controller as:
List<CheckItemClass> Models = new List<CheckItemClass>();`
And from documentation:
In particular it is legal for a variable, method, and a class within a class to have the same name.
So that's why you are not getting any compile errors with same names. It's always a good idea to keep names specific to the context.
And it's the same variable that's being used in your fillItems()
method as below:
public List<CheckItemClass> fillItems(Credit_review__c obj){
Models.add(new CheckItemClass('Miscellaneous (Google Search Items)',obj.GIS_Stip_Notes_Miscellaneous_Search__c,obj.GIS_Stip_Miscellaneous_Search__c));
return Models;
}
So Models.add()
is not really calling any method in the Models
class, but the List.add()
method. Also refer to the return statement which confirms as what you are returning.
edited Dec 14 '18 at 21:19
answered Dec 14 '18 at 21:06
Jayant DasJayant Das
16.1k2826
16.1k2826
thank you! I realized I missed theList<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!
– Olivia
Dec 14 '18 at 21:11
add a comment |
thank you! I realized I missed theList<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!
– Olivia
Dec 14 '18 at 21:11
thank you! I realized I missed the
List<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!– Olivia
Dec 14 '18 at 21:11
thank you! I realized I missed the
List<CheckItemClass> Models = new List<CheckItemClass>();
. This is a good example of how important naming is. It can get easily confusing!– Olivia
Dec 14 '18 at 21:11
add a comment |
Name shadowing can take many forms...
Here is my personal favorite (as I have made this mistake more than once wrto name shadowing) from the lazy loading pattern...
public Contact contacts {
get {
if (contacts == null) {
Contact contacts = [SELECT ... FROM Contact WHERE ...]; //oops - declared new vbl!
}
return contacts;
} private set;
}
if (contacts.isEmpty()) {...} . // null pointer exception!
add a comment |
Name shadowing can take many forms...
Here is my personal favorite (as I have made this mistake more than once wrto name shadowing) from the lazy loading pattern...
public Contact contacts {
get {
if (contacts == null) {
Contact contacts = [SELECT ... FROM Contact WHERE ...]; //oops - declared new vbl!
}
return contacts;
} private set;
}
if (contacts.isEmpty()) {...} . // null pointer exception!
add a comment |
Name shadowing can take many forms...
Here is my personal favorite (as I have made this mistake more than once wrto name shadowing) from the lazy loading pattern...
public Contact contacts {
get {
if (contacts == null) {
Contact contacts = [SELECT ... FROM Contact WHERE ...]; //oops - declared new vbl!
}
return contacts;
} private set;
}
if (contacts.isEmpty()) {...} . // null pointer exception!
Name shadowing can take many forms...
Here is my personal favorite (as I have made this mistake more than once wrto name shadowing) from the lazy loading pattern...
public Contact contacts {
get {
if (contacts == null) {
Contact contacts = [SELECT ... FROM Contact WHERE ...]; //oops - declared new vbl!
}
return contacts;
} private set;
}
if (contacts.isEmpty()) {...} . // null pointer exception!
answered Dec 15 '18 at 0:11
cropredycropredy
36k441122
36k441122
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242685%2fadding-a-wrapper-class-to-an-external-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown