Things to consider while writing the apex class
Public vs Global access modifiers:
Public :
The public access modifier declares that this class is visible in your application or namespace.
Global :
The global access modifier declares that this class is known by all Apex codes everywhere.
With Sharing & Without Sharing
With Sharing :
It’s Called User Context using with sharing keyword the apex code Runs the user context.
User context
If a particular object or field doesn’t have access then the apex code will show the error in DML operations.
Without Sharing:
It’s called system context, using without sharing keyword the apex code run in the system context. Apex code Access all the objects and fields
system context
If a particular object or field doesn’t have access then only the apex will execute the error in DML operations.
Constructor :
A constructor is a code that is invoked when an object is created. The main purpose of the Constructor is to perform the Initialization of an object.
Constructor Overloading:
Within a class, we can declare multiple constructors. And all these constructors have the same name and Different arguments.
Static block:
Static blocks are the block which is implicitly called when the class is instantiated which means when the instance of the class is made
It will execute before the default block and constructor.
Method Overloading in Apex:
If a class have multiple methods by the same name but different parameters.
Utility classes:
Utility classes are helper classes that consist of reusable methods. From triggers, we can call methods in such public classes. This helps to reduce code within the trigger and make it more structured.
Wrapper Class:
A wrapper class is a class, a data structure, or an abstract data type which contains different objects or collections of objects as its members. A Wrapper Class will help us replicate the same data structure in Apex.
Custom Setting:
Let you customize org data. It’s similar to Custom objects we can create files, Custom settings store catchy memory means easy to access. In the custom setting, we have only 10 MB of data.
Type of custom setting:
- Same Data for Every user in the Org.
- Data can be fetched based on the Profile and User.
- By default hierarchy type only enable, but if you want to use the list type.
- Go to Setup-> Shema setting-> to manage list custom setting- > enable
Custom Metadata:
Allows access without repeated queries to the database. Custom metadata also needs to create a custom field like a custom object. Custom metadata is similar to a custom setting except for this deployable across the environment. We should use a query but it won’t count in the governor limit.
Instance methods:
Instance methods (those missing the static
keyword) can only be called after an instance of the class is created. They should be used when there is an instance of the class to differentiate data between multiple copies of a class.