• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Preferences in java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know preferences is used for storing data.
But can somebody please explain the below code in layman's terms ?
Like what it does ?
It creates a file or something to store data ? How does it store?

Method 1 to create preferences object



Method 2 to create preferences object
This is to allow your package structure to define the tree structure. This option should not be used if your class is not in a package since the node will be "unnamed" and shared by every class not in a package.




What if every class shares the node ? What goes wrong ?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to your first question is: you don't know and you shouldn't care. This is implementation specific to the JVM and is not guaranteed to be consistent. For example, by default the preferences are stored in the registry on Windows systems and in a text file on *NIX systems. Since the implementation does not matter, you don't need to worry about exactly how it works. The only thing that you need to know is that it is stored outside of your application; that is important for understanding the answer to question #2.

Since the data is stored outside of your application (in the registry or in a text file, or somewhere else if you write your own implementation), it is available to multiple applications! If you don't keep the nodes separated by packages, you may find the preferences from one application bleeding into another. Sometimes this is useful (if you are working on integrated applications), but many/most times it is not. To ensure that you don't bleed this data, make sure you are using distinctive nodes (as defined by your package structure); the default node is not distinctive. (And if you are working on integrated applications, don't use the default node anyway; your applications can share a distinctive node by using method 1).
 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiv Tattva wrote:
But can somebody please explain the below code in layman's terms ?



I'm finding it hard to think of how to explain what it does without just quoting the java.util.prefs.Preferences documentation.

Preferences abstract a hierarchical implementation dependent persistence mechanism with a per-user and system wide root. A user node is independent of a system node and of another system user's user node.

Nodes have a root (path "/") and child paths. You can specify a path as a slash delimited String or have it defined by a class' package where '.' will be replaced with '/'. Parent nodes of a path are created if they do not already exist.

If you use suggested package naming conventions and the packaged based node look-up, you can expect to have your node to yourself and not have other code messing with your preferences. You are also free to make up your own unique path using the node(String pathname) method.

If two bits of code using Preferences share the same node and have different concepts of what should be stored in a key, you get unexpected results. If they have the same concept, it can be a nice way to reference common information.


There are times, like when troubleshooting on a target system, that it can be nice to know how it stores the data or at least where it is stored. You can find articles about that on the Internet with searches like java preferences mac if you wanted to know for the Mac OS.
 
Shiv Tattva
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jacob Anawalt , I did go through the documentation before creating this thread, but could not understand it very well
So here user node means a folder in which file containing user preferences exists ? And all the users have thier own node but share one system node ? right ?

And can you shed some light on this "Preferences abstract a hierarchical implementation dependent persistence mechanism with a per-user and system wide root" ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiv Tattva wrote: And can you shed some light on this "Preferences abstract a hierarchical implementation dependent persistence mechanism with a per-user and system wide root" ?



It means what you said:

So here user node means a folder in which file containing user preferences exists ? And all the users have thier own node but share one system node ?



except that it's a tree structure (that's what "hierarchical" means) and not just a "folder" containing a "file".
 
Shiv Tattva
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul Clapham,Joel McNary and Jacob Anawalt for your priceless responses ..
I feel so good now

 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic