Java util Preferences constructors -
i'm trying understand how different constructors java.util.prefs.preferences works, , not sure ones use under different scenarios.
for one, does preferences persist preferences/properties file? in other words, can use class store preferences across runs of application, or held in-memory?
as far constructors, there's few ways instantiate preferences:
preferences p = new preferences(); preferences p = preferences.usernodeforpackage(this.getclass()); preferences p = preferences.userroot(); the api docs constructors as-follows (respectively):
sole constructor.
returns preference node calling user's preference tree associated (by convention) specified class's package.
returns root preference node calling user.
so guess i'm confused relationship between different preferences instances, java classes (usenodeforpackage(class<?>) ???) , end user. ask: what each constructor/factory method differently other, , when use each?
thanks in advance!
"for one, preferences persist preferences/properties file?"
yes, data persistent. no not necessarily write them file. method of storage implementation defined, store them in remote database, registry or in actual file accompanying binary. on windows default implementation writes registry. (this written @ top of api doc linked)
"what each constructor/factory method differently other, , when use each?"
preferences p = new preferences(); this not directly callable @ojota84 mentioned in post, used factory methods below:
preferences p = preferences.usernodeforpackage(this.getclass()); this allows preferences implementation return preferences object specific given class , doesn't interfere preferences other classes. on windows typically returns preferences object stores keys hku\software\javasoft\prefs\myapplication\myclass\. in xml backed file correspond <myapplication><myclass>-data here-</myclass></myapplication>.
preferences p = preferences.userroot(); this similar above 1 returns root object user preferences. if think of preferences arranged tree. makes sense. on windows typically implemented hku\software\javasoft\prefs\myapplication\. xml analogy you'd accessing <myapplication>-data here-</myapplication>.
choosing between userroot() , usernodeforpackage()
in essence matter of preference (pun not intended). if need store 1 or 2 keys, userroot() create less entries in registry. if need write lots , lots of keys, usernodeforpackage() better avoid name clashes , other problems. clearer structure in whatever backing preferences, xml file, db or windows registry.
Comments
Post a Comment