Lazy load class variables Following my java class is required to be reviewed and couple of clarifications about class variable loading is required. Following is my class.  

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidateUtil{

    private static Pattern pattern;     
    private static final String REFFERENCE_PATTERN = "The regex pattern here";        

    public static boolean validate(final String reference){           
    if(pattern ==null){
      pattern = Pattern.compile(REFFERENCE_PATTERN ); //Line 10
    }   

      Matcher  matcher = pattern.matcher(reference);
      return matcher.matches();             
    }

     /* Many util Methods here
      *       
      */    

}


There is only one method shown above, but there are many static utility methods are in this class. Based on the requirement, those methods are invoked.
In above class, variable pattern is declared, but not initialized. Instead, it is initialized inside the method validate(). The purpose of initializing so is to avoid initializing unnecessarily and initialize variables or create objects only if they are required and they are going to be used. That means, if it is defined  

private static Pattern pattern = Pattern.compile(REFFERENCE_PATTERN );  


as a class variable (not initialize inside the method), there will be a pattern object created whether the validate() method is used or not.  Actually, this is only one sample, there can be many situations like variables for supporting Object or array of Objects.  

My question is that, is doing so is actually worth in a code ? This may lead to increase the line of codes and sometime, the performance gain can be neglectable (For our own custom objects). On the other hand, is this really worth only for heavy, large objects (like Database connections)?
First I want to suggest a concrete implementation:

public class ValidateUtil{

    private static Pattern pattern;     
    private static final String REFFERENCE_PATTERN = "The regex pattern here";        

    public static boolean validate(final String reference){           
        return getPattern().matcher(reference).matches();
    }

    public static Pattern getPattern() {
        if (pattern == null) {
            pattern = Pattern.compile(REFFERENCE_PATTERN);
        }   
        return pattern;
    }

    /*
     * Many util Methods here       
     */    

}



  This may lead to increase the line of codes


Lines of code is at the most an indicator of less code quality. Do not ask for lines, do ask for responsibilities. Hypothetically a method/a class can have thousands lines of code and it will not produce any problem if it has only one responsibility. But I'll give you that it becomes more and more probable to violate the SRP (single responsible principle) the more code is available in the scope. And of course you may have other problems can be derived from many lines of code available. But code of lines alone will say you less to nothing.


  and sometime, the performance gain can be neglectable


And here you got a point. In one of my projects I discussed using "synchronized" and the performance issues. We measured a relation about 1:20. So of course it MUST be an issue... We made a second measurement. We iterated over a synchronized method 20.000.000 times and with the available pc configuration it took 20 seconds. BTW, the methods we talk about were called in maximum 20 times a second. What I want to say is: It really depends on the use case. But in MOST use cases you do not need not care about preemptive optimization. And you have to look at both: relative and absolute metrics.


  On the other hand, is this really worth only for heavy, large objects (like Database connections)


A very clear answer in THIS case of LAZY INITIALIZATION: No.

It may be a case of preemptive optimization. But that is not the reason I would prefer it. The reason why I would do so is: If you only use lazy getter-methods within or from outside your object/class you make yourself independent of the order of object creation. You use the getter and you get an object, doesn't matter if it was created before or it was created right now. This is especially useful in component trees. You often play around with the position of an ui element in the ui component tree. A lazy getter saves your ass and time. After all ui components often are heavy and even if they are not you get the optimization for free.