• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding Java Bean Using Constants Instead of Variables

 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bhavani Sankar Ikkurthi
Greenhorn
Posts: 8
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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:



Output:

REQUEST_INSTANCE_ID = Hello There!
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic