jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Coding Java Bean Using Constants Instead of Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Coding Java Bean Using Constants Instead of Variables" Watch "Coding Java Bean Using Constants Instead of Variables" New topic

Coding Java Bean Using Constants Instead of Variables

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I have coded Java bean and its getters and setters.

and in other Java class, I code the way below to set values:

Now, I am told to set values in a different way:

How do I change my code to set values in the way above?

Thanks for your help.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Ugghh. Who made that brilliant decision? <--- dripping with sarcasm

If I were forced at gunpoint to do this, I'd store the values in a Map using the constants as the keys.

Be aware that your class is no longer a Bean in that case, as it violates every bean convention (in addition to common sense).

[Asking smart questions] [About Bear] [Books by Bear]
Bhavani Sankar Ikkurthi

Joined: Aug 07, 2008
Posts: 8

I think that is an old fashioned way of doing it. (very old fashioned may be)
You will have no more getters and setters. As Bibeault said, store them in a Map and then send that over to the set method. [set(map mP)].
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Ah, the good old java.util.Calendar approach... The difference is, Calendar uses the same type for all values you can get using the fields. The only reason they have started using this approach is the number of methods you would get - I believe there are about 14 or so fields, so that would be 14 method sets with set, get, add and roll, giving you at least 56 methods. Ouch.

But for cases where the types differ or the number of methods would be relatively small, I strongly suggest not to use this approach.

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46402
. . . and if you are passing ints or similar to determine which field to set, you can use a switch-case block for the actual setting.
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Hi, I have talked to my manager. I have to do what I am told to do. How do I put all the properties of the Java bean in a Map? I have to do something like:

Parameters param = new Parameters();
param.set( REQUEST_INSTANCE_ID, requestInstanceID );
param.set( REQUEST_START_TIME, requestStartTime );

The "requestInstanceID" and "requestStartTime" are the properties in a Java Bean. How do I change my Java Bean? Please help. Thank you.
Catalin Roncea

Joined: Sep 15, 2011
Posts: 2

This is my first post .

You will not need the vars requestInstanceID and requestInstanceID and their getters and setters anymore.
You can create a Map for example HashMap in your "Parameter" class:

and then 2 String constants for each parameter for ex:

Then you should create 2 methods (one for setting a value and one for getting a value from the HashMap).
Here is the set method:

The get method will return the value for the key provided. Something like:

Here is the test in the main() function:


REQUEST_START_TIME = Thu Sep 15 12:13:19 EEST 2011

You should read the java documentation for Map, HashMap for further details. You can change Hasmap with other Map implementation.

I hope it helps

John Jai

Joined: May 31, 2011
Posts: 1776
Catalin Roncea wrote:// Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

Thanks for the info!

and welcome to the Ranch
I agree. Here's the link: http://aspose.com/file-tools
subject: Coding Java Bean Using Constants Instead of Variables
It's not a secret anymore!