• Post Reply Bookmark Topic Watch Topic
  • New Topic

Errata - OCP Java SE 8 Study Guide - page 265  RSS feed

 
onur otlu
Greenhorn
Posts: 15
3
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my best knowledge; getting value of a key from any parent of the matching resource bundle mentioned in the book should be revised. Besides, the first row is correct and the secod row is incorrect in Table 5.8, and the correction in errata list indicating "Table 5.8 is incorrect. In the first row second column, Zoo_fr.properties and Zoo.properties should be included as well." is unfortunately incorrect.

If there exist a java class resource bundle in any place of the parent of the matching resource bundle, Java disregards the property file resource bundles from at that point to the top of the hierachy either matching resource bundle is a java class file or a property file. That is to say; when Java reaches the first java class resource bundle in hierarchy during the process finding a match for a key, then Java just looks at java class resource bundles in the remaining part of hierarchy, and omits property file resource bundles.

I'll give some examples to explain in detail what I mentioned above.
Let's set locale as "fr_FR" and define "Zoo_fr_FR.properties", "Zoo_fr.properties", "Zoo.java" and "Zoo.properties" files. Lets "Zoo.properties" file include a key others don't have. If we try to access value of that key, Java will throw a MissingResourceException. Java determines "Zoo_fr_FR.properties" file as matching resource bundle. Then it looks for the key in "Zoo_fr_FR.properties" file and it fails, then it looks for the key in "Zoo_fr.properties" file and it fails again. Then it looks for the key in "Zoo.java" class file, and it fails again. After investigating the first java class resource bundle, it disregards "Zoo.properties" file and throws a MissingResourceException.

Let's set locale as "fr_FR" and define "Zoo_fr_FR.java", "Zoo_fr_FR.properties", "Zoo_fr.properties", "Zoo.java" and "Zoo.properties" files. Lets "Zoo_fr_FR.properties" file include a key others don't have. If we try to access value of that key, Java will throw a MissingResourceException. Java determines "Zoo_fr_FR.java" file as matching resource bundle. Then it looks for the key in "Zoo_fr_FR.java" file and it fails. After investigating the first java class resource bundle("Zoo_fr_FR.java"), it disregards "Zoo_fr_FR.properties" and  "Zoo_fr.properties" files. Then it looks for the key in "Zoo.java" file and it fails. It disregards "Zoo.properties" file and throws a MissingResourceException.

If we return to Table 5.8, the first row works how it is explained, however the second row looks the files in Zoo_fr.properties and Zoo.java sequentially, and disregards Zoo.properties file.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36171
439
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right! I had no idea it worked that way.

I tried a simpler example to illustrate the problem:



File rb.Zoo_en.properties




This runs printing
a
c

This shows that Java does in fact look at a Java file after a property file. Wow.
 
onur otlu
Greenhorn
Posts: 15
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to give an example to describe what I mentioned above. The system will determine "Zoo_fr_FR.properties" file as matching resource bundle when we request a resource bundle by giving Locale("fr", "FR").

I have defined below resource bundles.
Zoo.java,
Zoo.properties,
Zoo_en.java,
Zoo_en.properties,
Zoo_en_EN.java,
Zoo_en_EN.properties,
Zoo_fr.java,
Zoo_fr.properties and
Zoo_fr_FR.properties files.

I did not define "Zoo_fr_FR.java" file. Each file includes a single explanatory key-value pair that is similar to file name.


Zoo.properties

Zoo_en.properties

Zoo_en_EN.properties

Zoo_fr.properties

Zoo_fr_FR.properties


Zoo.java


Zoo_en.java


Zoo_en_EN.java


Zoo_fr.java


If we examine the below code, the system prints the first one successfully but the second one will throw an exception. Firstly, Java tries to find the key "open_Zoo_fr.properties" in "open_Zoo_fr_FR.properties" file. It cannot find it, then it looks in "open_Zoo_fr.java" file for the key. Then it fails again and at that point it just look at the java class resource bundles on the path to the root (it eliminates the properties file resource bundle).

If you remove the java class resource bundles from the project and if you run it again, it succesfully finds both keys and prints their values.


MatchingPropertiesFileExample.java
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36171
439
Eclipse IDE Java VI Editor
 
Julien Belaid
Greenhorn
Posts: 1
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

If i may i would to correct the explanations given above regarding the priority between the java class and the properties files

onur otlu wrote:when Java reaches the first java class resource bundle in hierarchy during the process finding a match for a key, then Java just looks at java class resource bundles in the remaining part of hierarchy, and omits property file resource bundles. 


Onur it is not totally right. The java class only hides the property file for the SAME level of the hierarchy, and not all the way to the top. After finding a java class in the hierarchy, you can still pick a property in a property file, but higher in the hierarchy.
In fact, from the matching resource bundle, java will create a hierarchy to the top, and at each level, will choose the java class over the property file, and continue the process for the parent, and so on.

To sum up, if you have a property file with the exact same name as the java class (Zoo_fr.properties, and Zoo_fr.class), the property file is useless and can never be accessed.

Consider the following example for the Locale "fr_FR", and the following files

Zoo_fr_FR.properties   <== Matching resource file


Zoo_fr.class   <== Direct parent of Zoo_fr_FR.properties


Zoo_fr.properties   <== Hidden by Zoo_fr.class, can never be accessed


Zoo.properties   <==  Direct parent of Zoo_fr.class, properties are available if not overidden by a child


And the main



Thanks for reading


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36171
439
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for contributing that; have a cow. And welcome to CodeRanch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!