Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026 Agenda Now Available: Explore Sessions
cancel
Showing results for 
Search instead for 
Did you mean: 
PhilHibbs
Creator II
Creator II

Defining a static variable

I want to create a static variable that I can initialize once and then re-use. There are a few places I want to do this - a compiled regular expression, a SimpleDateFormat, there are a number of objects in Java that are useful to create once and re-use.

 

At the moment I am creating the objects in a tJava component and pushing them into the globalMap or a sharedMap object. That means that every time I access one of them, I have to pull it out of the map and cast it to the right class.

 

Is there a better way of doing this?

Labels (2)
1 Solution

Accepted Solutions
PhilHibbs
Creator II
Creator II
Author

I solved my problem by creating a code entry like this:

public class statics_for_phil {
    public static java.text.SimpleDateFormat format_YYYYMMDD;
    
    public static java.util.regex.Pattern    file_name_split;
    
    static {
        format_YYYYMMDD = new java.text.SimpleDateFormat("yyyyMMdd");
        file_name_split = java.util.regex.Pattern.compile("^S3_(\\d+)_([a-z]+)_(\\d+)");
    }
    
}

View solution in original post

1 Reply
PhilHibbs
Creator II
Creator II
Author

I solved my problem by creating a code entry like this:

public class statics_for_phil {
    public static java.text.SimpleDateFormat format_YYYYMMDD;
    
    public static java.util.regex.Pattern    file_name_split;
    
    static {
        format_YYYYMMDD = new java.text.SimpleDateFormat("yyyyMMdd");
        file_name_split = java.util.regex.Pattern.compile("^S3_(\\d+)_([a-z]+)_(\\d+)");
    }
    
}