In my organisation , they ve written a class and in description it says " This is Utility Class " . Actually what is the purpose of utility class ? Why are we using that ?
The purpose of a utility class is to serve as a single one-stop-shop for reusable pieces of code. Basically any variable, method, inner class and whatever it might be that is used multiple times across multiple other classes within the project, can reside into a single (or multiple) utility class(es), which helps writing more elegant and structured code, as well as makes the code easier to maintain.
Utility classes are your friend. In addition to @Bachovski use cases, my Util methods provide centralized solutions for caching reference data to save on SOQL calls - especially when you have a complex environment wherein triggers fire on multiple objects per transaction. So, for example, sometimes you have code that needs to execute logic that varies based on running user's Profile name.
The Util class method
getProfile(someName) will return the Profile object for a given name but cache all the Profiles on the first invocation of
Util.getprofile(..) as the Profiles don't change in a given transaction but multiple triggers/handler classes may need the profile name for logic switching.
You'll find that as you move from org to org, that you'll copy over your Util methods and build on them -- within months/few years - you'll have a robust set of commonly-executed functionality that will save on time and make your calling logic cleaner and more consistent.
Utility class contains functions or methods which might be used frequently in a lot of class in the SFDC instance. eg. we want to get the label or API Name of any custom objects or to get the name of the record type from the id.
We write a generic method in the utility class which does the job for you and you can reuse the code of that class in any other class.