I have such class written by some other developer:
public class ManifestFile implements Serializable {
private final static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
private final static XPathFactory xPathFactory = XPathFactory.newInstance();
private final static DateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
private final String uuid;
private final Set<File> attachments = new LinkedHashSet<File>();
private final transient ApplicationContext applicationContext = JavaService.INSTANCE.getApplicationContext();
private final transient File attachmentDirectory;
private final Date processAfter = new Date(System.currentTimeMillis() + 3 * 1000 * 60);
{
try {
documentBuilderFactory.setNamespaceAware(true);
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(getClass().getResourceAsStream("/StrategicEmail5.xsd")));
documentBuilderFactory.setSchema(schema);
documentBuilderFactory.setValidating(true);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
I am surprised by this part:
{
try {
documentBuilderFactory.setNamespaceAware(true);
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(getClass().getResourceAsStream("/StrategicEmail5.xsd")));
documentBuilderFactory.setSchema(schema);
documentBuilderFactory.setValidating(true);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
Could someone please explain is this code valid and what is the advantage of using {} outside any method body?