You need to sign in to do that
Don't have an account?
DJ 367
Can someone please help me to understand inner class concept in details
Hello All,
Can you please help me to understand inner class concept in details in apex. I have below code which is fine but I am not able to understand the concept. Here is my code. Thanks
Can you please help me to understand inner class concept in details in apex. I have below code which is fine but I am not able to understand the concept. Here is my code. Thanks
//Apex class public with sharing class InputSelectErrorExt { // parent standard controller private ApexPages.StandardController stdCtrl {get; set;} // object to encapsulate the entered value and any error // message associated with it public ValueAndError accountIdVal {get; set;} // constructor public InputSelectErrorExt(ApexPages.StandardController std) { stdCtrl=std; accountIdVal=new ValueAndError(); } // Creates select list options for all accounts in the system public List<SelectOption> getAccountOptions() { List<SelectOption> result=new List<SelectOption>(); result.add(new SelectOption('', '-- Choose --')); for (Account acc : [select id, Name from Account]) { result.add(new SelectOption(acc.Id, acc.Name)); } return result; } // validates the input and generates an error message or // delegates to the standard controller save method public PageReference save() { PageReference result=null; // clear down the previous errors accountIdVal.error=''; if (String.IsBlank(accountIdVal.value)) { accountIdVal.error='Please choose an account'; } else { result=stdCtrl.save(); } return result; } // custom class to encapsulate a value and error message public class ValueAndError { public String value {get; set;} public String error {get; set;} public Boolean getHasError() { return (!String.IsBlank(error)); } } }VisualForce Page
<apex:page standardController="Contact" extensions="InputSelectErrorExt"> <apex:form > <apex:sectionHeader title="Create Contact" /> <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}" /> <apex:commandButton value="Cancel" action="{!cancel}" /> </apex:pageBlockButtons> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:outputLabel value="Choose account" /> <apex:outputPanel > <apex:selectList value="{!accountIdVal.value}" size="1" styleClass="{!IF(accountIdVal.hasError,'error','')}"> <apex:selectOptions value="{!accountOptions}" /> </apex:selectList> <div class="errorMsg" style="display:{!IF(accountIdVal.hasError,'block','none')}"><strong>Error:</strong> {!accountIdVal.error}</div> </apex:outputPanel> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem /> <apex:inputField value="{!Contact.Salutation}"/> <apex:inputField value="{!Contact.FirstName}"/> <apex:pageBlockSectionItem /> <apex:inputField value="{!Contact.LastName}"/> <apex:inputField value="{!Contact.Phone}"/> <apex:inputField value="{!Contact.Email}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
"We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of the outer class including private data members and methods".
Nested Classes. just like methods, variables of a class too can have another class as its member.
Writing a class within another is allowed in Java. The class written within is called the nested class,
and the class that holds the inner class is called the outer class.
"same in your case, if you want to save without select any account then it returns zero from inner class otherwise return one."
Thank You,
Ajay Dubedi
All Answers
Please mark it as solved if my reply was helpful, it will make it available
for others as a proper soultion.
Best Regards,
Sandhya
Try this one
"Inner class is helper/utility class, keeps code lean and clean. In practice, I have mainly used inner class with Visual Force Controllers, mainly to represent data to View (VF Page) in the desired way outside of SObject's structure stored".
Apex Class Definition
In Apex, you can define top-level classes (also called outer classes) as well as inner classes, that is, a class defined within another class. You can only have inner classes one level deep. For example:
1
public class myOuterClass {
2
// Additional myOuterClass code here
3
class myInnerClass {
4
// myInnerClass code here
5
}
6
}
To define a class, specify the following:
Access modifiers:
You must use one of the access modifiers (such as public or global) in the declaration of a top-level class.
You do not have to use an access modifier in the declaration of an inner class.
Optional definition modifiers (such as virtual, abstract, and so on)
Required: The keyword class followed by the name of the class
Optional extensions and/or implementations
Deserialization:-
Deserialization is much simpler using an inner class, though you can use top-level classes as well.
public class TopLevel
{
public class SomeStructure
{
public String someProperty;
}
}
Then you can deserialize using this class like:
SomeStructure data = JSON.deserialize(
'{"someProperty": "<value>"}',
TopLevel.SomeStructure.class
);
Also, you can refer the below code.
Thank You,
Ajay Dubedi
Thanks for your precious reply. However I just want to understand what is the purpose of Inner class. Like I have posted my code . Can you please explain me why inner class it used there in the code.
Thanks a lot.
"We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of the outer class including private data members and methods".
Nested Classes. just like methods, variables of a class too can have another class as its member.
Writing a class within another is allowed in Java. The class written within is called the nested class,
and the class that holds the inner class is called the outer class.
"same in your case, if you want to save without select any account then it returns zero from inner class otherwise return one."
Thank You,
Ajay Dubedi