I'm trying to create a static utility class which will contain simple functions like Messagebox(AlertDialog), EmailSender, etc... and will call these functions on other activites. But as I understand I can not create a static class if it is not in a class. What do you suggest for this kind of utility classes?
            Asked
            
        
        
            Active
            
        
            Viewed 4,362 times
        
    1
            
            
        - 
                    what about using a Singleton? I frequently use this pattern if I want to have statefull shared functionalities among different activities. – elbuild Oct 19 '13 at 14:55
- 
                    Can you give an example please. – sa_ Oct 19 '13 at 15:00
- 
                    What you should do is to create a singleton properly by: http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – zegnus Oct 19 '13 at 15:35
- 
                    right way to create utility class http://stackoverflow.com/a/31581218/3496570 – Zar E Ahmer Jul 23 '15 at 07:50
1 Answers
4
            Why not make it that way
class Utils{
    public static void makeToast(Context context, String text){
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}
In your activity call it this way
Utils.makeToast(this,"hi");
 
    
    
        A.S.
        
- 4,574
- 3
- 26
- 43
- 
                    A better approach for a utilities class is to make the class `final` and add a `private` constructor. – jt-gilkeson Jun 25 '15 at 20:15
