• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Constants Class

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

What is the feeling in this forum about constants?

For things like the title of the frame of the gui Ddo you all think that it is better to:

a) create a constants class containing these constants such as:

class Constants {
public final static String GUI_TITLE = "Bogitt and Scarper Application";
}

b) just put the string into the method that uses it since it's only going to be used once:

class GUI extends JFrame {

...

setTitle("Bogitt and Scarper Application");

...

}

c) Or shall I just have it as a constant in the class in which it is to be used?

class GUI extends JFrame {

...

private final static String GUI_TITLE = "Bogitt and Scarper Application";

...

}

Cheers
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use (b). The reason is those strings are more likely to come from resource bundle files.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In my case, I separated all texts for GUI labels, exception messages, etc. into a resource bundle, a properties file.

For example, in the properties file, say, "MyProj.properties" under the "suncertify" directory, you will have a line:


Then, in the program code (JFrame), you access the value in the following way:


(Note it's a common practice using the resource bundle in web component development for internationalization (i18n) purpose, etc.)

Chulwoo
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things came to mind when I read this post.

First of all, most String values should come from resource bundles, so that limits the scope of the question. But even the name of the resource bundle will be a String, so that String has to be placed somewhere as well.

I actually prefer to use an interface to declare all of my constants, as opposed to a class. Just a style thing. And just to clarify, I'm not recommending the Constant Interface Java anti-pattern where classes implement the interface to access the constants. That's just plain bad.

For me, the big point is that String values should not be found in the body of code. String values are not 'type-checked', which is of huge significance in any sized project, they are often used in different places by different users, which can cause confusion, and they often need to change, so having them all defined in one place makes sense.

You should take my advice. I have a Science degree, and my concentration was String theory.

Cheers!

-Cameron McKenzie
[ November 07, 2006: Message edited by: Cameron W. McKenzie ]
 
Mike Ngo
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

setTitle(java.util.ResourceBundle.getBundle("suncertify/MyProj").getString("application.title"));



You should make a class to read the resource bundle or you will have to change lots of files if you ever change the name of your properties file.
 
Chulwoo Choi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few comments:

I actually prefer to use an interface to declare all of my constants, as opposed to a class. Just a style thing.

For me, the big point is that String values should not be found in the body of code. String values are not 'type-checked', which is of huge significance in any sized project, they are often used in different places by different users, which can cause confusion, and they often need to change, so having them all defined in one place makes sense.

You should take my advice. I have a Science degree, and my concentration was String theory.


1. It is recommended not to use interfaces for constants. For example, from Effective Java (by Bloch):

Item 17: User interfaces only to define types
...
The constant interface pattern is a poor use of interfaces.
(explanation follows in two pages)
...


2. You can have a constant for the file name and use it (in JFrame's setTitle(...) for example) if you are worried about file name change in the future:

Resource bundles are known to be widely used for text labels in applications.
User preference is another thing. See below.
3. For user preference values, you can consider using:

It provides methods for values of various types.
(But this class is NOT for SCJD project as you're required to use a properties file (at least according to my requirements doc).)

Regards,
Chulwoo
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have saved all the Labels in the suncertify.properties file itself and am using it to store both the labels and the user preferences. I know it is stupid to merge both but then a MUST made that decision, <B>must have only one properties file</B>
 
Chulwoo Choi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,

You said:

...
must have only one properties file
...



In my instructions document:

...
configuration information must be stored in a file called suncertify.properties
...


This sentence doesn't imply suncertify.properties must be the only properties file. I think it should be okay to use this properties file for configuration purpose and another one for GUI labels, etc.

Chulwoo
 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me that we can use the java.util.Properties class to do the reading and writing from our properties file(s).
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's part of the standard Java API, so I see no reason why you can't use it. I used it.

I defined various application strings in a Constants class. Bad practice, I know.

Jeremy
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Jones:
Please tell me that we can use the java.util.Properties class to do the reading and writing from our properties file(s).



we can use the java.util.Properties class to do the reading and writing from our properties file(s).

happy?

For reading resourcebundles you should use ResourceBundle instead though, as it handles things like internationalisation semi-automatically by selecting the locale to load based on user OS language and available bundles.

But I think that's overkill for an application on this scale and coding the titles into the screens as fixed strings would be quite acceptable.
 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's the difference between using a resource bundle and the properties class. Isn't the latter simpler?
 
Chulwoo Choi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith,

1. When to use which:
For text labels, e.g. JFrame's title, button's label, etc., use resource bundles.
For configuration values, e.g., port number (an int value), DB file path (a String value), etc., use java.util.Properties.

2. Similarity:
They (resource bundle and config properties) both use a properties file that contains a list of key/value pairs:


3. Differences:
For labels, key/value pairs are stored in properties file and this properties file is read only. You just use the texts in the file.
For config values, key/value pairs are read/written from/to a properties.

Note labels and config values are used for different purposes. For example, button labels are not config values and DB file path is not a label (it is a data used in operating the application).

4. Requirements
For sun developer exam, you must save/retrieve config values to/from a properties file.
You do not have to use resource bundle for texts. But resource bundles are easy enough to use (see my post above) and are sometimes required in Java EE projects. So why don't you consider using it?

5. Alternatives
For config values, it is recommended to use java.util.prefs.Preferences instead of java.util.Properties. Preferences is similar to Properties but with Preferences you don't have to worry about properties file (files are created automatically by Preferences).
But you CANNOT use Preferences class as you have to use a properties file for SCJD project.

Regards,
Chulwoo
 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chulwoo you're very helpfull.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic