Should I change the line of code or stick to the current one?
According to your teacher, yours must.
Perhaps he wants you to study different approaches to the same thing.
So far, in the example you provided, this does not make much sense, it would be useful to use other types of messages or for i18n
Think about it:
String message = ResourceBundle.getBundle("messages").getString("user.notfound"); throw new UsernameNotFoundException(MessageFormat.format( message , new Object[] {username}));
You may have messages_en.properties and messages_es.properties file
The first with the line:
user.notfound=Username '{0}' not found!
And the second one with:
user.notfound=¡Usuario '{0}' no encontrado!
Then that would make sense.
Other uses of MessageFormat are described in the doc.
MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}."); double[] filelimits = {0,1,2}; String[] filepart = {"no files","one file","{0,number} files"}; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); form.setFormatByArgumentIndex(0, fileform); int fileCount = 1273; String diskName = "MyDisk"; Object[] testArgs = {new Long(fileCount), diskName}; System.out.println(form.format(testArgs));
Output with different values for fileCount:
The disk "MyDisk" contains no files. The disk "MyDisk" contains one file. The disk "MyDisk" contains 1,273 files.
So perhaps your teacher lets you know about the opportunities that you have.