Encapsulating Java Preferences API

I used to have a custom preferences class for my applications. For my next hobby project i wanted to switch to the Preferences API. But the put and get functions require a default value and i do not want to spread default values all over the source files. Even though my project is small i can not imagine changing default values all over the source code. How do you guys use the api? I am thinking of wrapping the preferences api in another class but then what is the point of using the API because it only takes away the burden of saving the file to disk, which isn't that hard using serialization? Am i missing the point?


Asked by: Freddie101 | Posted: 23-01-2022






Answer 1

You're mixing a few concepts here. The default given in the code should be specific to the local situation as a 'reasonable default'. If you want to have application-wide defaults, then you need a preference-provider that allows you to hook in both the default preferences and an overlaid user-preferences. Something that may be a worthwhile project in itself.

Oh, and "reasonable defaults" is a great way to avoid configuration when it's not necessary but allow the user or packager to provide better values when needed.

@comment, I think I understand.

By 'local situation' I mean in the context of the code. For your GUI, you need a value for display that represents whatever the thread is using. Therefore I'd use something like Worker.DEFAULT_TIMEOUT. Your worker would use the same value internally as the default. That way you are retrieving the configured value or the worker's default when you are setting the worker's behavior.

Answered by: Jared225 | Posted: 24-02-2022



Answer 2

then you might want to take full control over how Preferences should follow your desired storage by implementing AbstractPreferences. You can see a linux based implementation here:

http://www.docjar.com/html/api/java/util/prefs/FilePreferencesImpl.java.html

Good luck!

Answered by: Briony147 | Posted: 24-02-2022



Answer 3

Would it be so hard to stick all your defaults in a single class so that they weren't littering your code?

I've used commons configuration in recent projects. I've looked into the Java Preferences API but I like the flexibility of the Commons project. And you don't have to specify default values!

Answered by: Alissa897 | Posted: 24-02-2022



Answer 4

You can put default values in .preferences file which you bundle in your .jar file (or in specialized class or interface with constants).

I use it for things like window positions/sizes, remembering default folder for choosing files, last opened files and such trivia. I can think of some interesting things you get "for free" with preferences API:

  • doing things in OS-recommended way; OS might not allow you to write "settings files" in your app folder, and users don't like to be asked where on disk they want to save settings, and you don't want to implement your custom logic for every platform
  • your data is saved for each OS user separately
  • a way to keep user data even if your application is uninstalled (or during an upgrade)
  • don't need database or access to file system

Also, I don't like serialization and don't recommend it for this. Serialization means you have to take care when changing your classes in new versions of your application.

Answered by: Adelaide853 | Posted: 24-02-2022



Similar questions

encapsulation - Access levels of java class members

I realise that this is a very basic question, but it is one which has always bothered me. As I understand things, if you declare a field private in Java then it is not visible outside of that class. If it is protected then it is available to inherited classes and anything in the same package (correct me if either of those definitions is incorrect). Does this mean it is not possible to declare a field that is acces...


encapsulation - Why is java secure coding important?

I'm having trouble understanding why java secure coding is important. For example, why is it important to declare variables private? I mean I get that it will make it impossible to access those variables from outside the class, but I could simply decompile the class to get the value. Similarly, defining a class as final will make it impossible to subclass this class. When would subclassing a class be dangerous for security...


oop - Advise requested regarding proper use of encapsulation in Java

I'm creating a little puzzle game just as a hobby project but the project has now reached a point that there is quite a lot of code (about 1500 lines). Although I've tried to prevent it, the code has become messy. I definitely want to clean up the code and make it more maintainable and readable while I still can. There are 3 classes handling actions regarding puzzle pieces in my game: class PieceCon...


java - In which scenarios do you use encapsulation?

I would like to know in what scenarios you use encapsulation. The purpose of this question is collaborative. So feel free to share your own experience when the subject is encapsulation. Some scenarios: Calculated property public class Order { private List<ListItem> listItems = new ArrayList<ListItem>(); public double getTotal() { ...


Confused with Java Encapsulation Concept

Good day! I am reading a Java book about encapsulation and it mentioned the getter and setter method. I've read that to hide the attributes, I must mark my instance variables as "PRIVATE" and make a "PUBLIC" method of getter and setter to access the data. So I tried making a similar but not the conventional code about it as follows: public class...


java - What's meant by "Data Encapsulation"? Use private data fields or methods?

Say I have a ClassA and ClassB extends ClassA ClassA { private int x; private void m1() { } } ClassB extends ClassA { } Under data encapsulation, we should private the data field and methods. Is that correct? But, in this case, ClassB can not use ClassA's data field and methods. So, under d...


java - Do both these classes support encapsulation and ...?

public class Normal { public string name; // name is public public String getName() { return name ; } public String setName(String newName) { name = newName ; return name ; } public static void main(String args[]) { Normal normal = new Normal(); normal.setName("suhail gupta"); System.out.println( "My name is : " + normal.getName() ); } } ...


java - encapsulation means putting related items together?

I was going through an article about Object Oriented Programming and it stated that encapsulation means putting related items together, but I don't understand how the article's representative example: UserProfile.js. Though this example is in JavaScript, I'm looking to understand these concepts in Java. Can ...


Encapsulation in Java

This question already has answers here:


java - JAXB via NetBeans - Can I Use Encapsulation?

I'd like to share a bunch of inner classes between two JAXB Schema defined classes. Is this possible? The goal is to be able to assign the instances of the inner classes from one Schema defined instance of an object of type X to another Schema defined instance of an object of type Y. I am noting I am using NetBeans because I am using their interface to the JAXB toolkit to do this. Sometimes that messes things up...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top