D. The code throws an exception at runtime.
Book's answer: D. Since no locale is specified, the code tries to find a bundle matching the default locale. Two resource bundles match the language English. The Java class one is used since it is present. However, it does not contain a key color , nor does its parent. Java does not allow looking in a properties file resource bundle once it has matched a Java
class resource bundle. Therefore, it throws a MissingResourceBundleException , and Option D is the answer.
...and it printed, “black”, didn't it? I got the same result. Have you looked on the book's website? I didn't find anything for page 381. Have you run the code with Java8?
I did and still got, “black” with Java8.
I'm not convinced this is an errata. I'm also a bit puzzled how both of you are getting "black." I think there is some subtle detail that is different in the example posted here than the one in the book.
My trying it I created three classes and one property file in the default package. (I din't bother creating Buggy.properties since it isn't relevant to the problem we are discussing.
Running PlayTest prints
Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle Buggy, key color
Testing for typos I then renamed Buggy_en.java to Buggy_english.java. When re-running, i did get "black". This shows the property file was in my classpath.
Conceptually Further, the example is *supposed* to throw an exception. There's two lookups that go on. The first one is to find the resource bundle to use. I think we both agree that is Buggy_en.java (due to it being the default Locale and java/list resource bundles having a high precedence than property file ones.)
The second is for lookjng up the keys. Java uses the parent resource bundle in the hierarchy if they key isn't found.A property file is not in the parent hierarchy of a java/list bundle. The Javadoc describes this as "hiding" the resource bundle:
The file MyResources_fr_CH.properties is never used because it is hidden by the MyResources_fr_CH.class.