Q:

Controlling when object is created

Suppose i need to create an object as follows and set some values
    FileMetaData fileMeta = fileContainer.getMetaData();

    fileMeta.setFileName("file name");
    fileMeta.setServer("my box");
    fileMeta.setDirectory("/path/to/dir");
    fileMeta.setFileType(FileType.PROPERTIES);

I later intend to use this object reference to do something useful.
I'd like to recognize the fact that it is possible for the user of the system to not set some fields, for instance, one may forget to 
    fileMeta.setDateModified(12345);

Is it somehow possible to guarantee that all (or some specific) fields are set before making the object available?

A:

There is nothing in the language to enforce this (except for having a lone visible constructor that takes all the required parameters), but you can do it idiomatically, with a variation on the builder pattern and some method chaining:
FileMetaData fileMeta = new FileMetaDataBuilder(fileContainer.getMetaData())
    .setFileName("file name")
    .setServer("my box")
    .setDirectory("/path/to/dir")
    .setFileType(FileType.PROPERTIES)
    .build();

The build() method can ensure that all the required fields are set before calling the appropriate constructor of FileMetaData with all the required parameters.

