• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Coding Java Bean Using Constants Instead of Variables

 
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.
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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).
 
Greenhorn
Posts: 8
Mac OS X Eclipse IDE Java
  • 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)].
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Marshal
Posts: 64666
225
  • 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.
 
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

 
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!