and I have instantiated this class in a different class but so far I can access fields of UserRegistration class like this way
UserRegistration object = new UserRegistration();
String str = object.first_name;
It shows that I cant use this object as an aaray like
String str = object[0] ;
My intention is to use this UserRegistration's object as an array so that I can iterate through its fields using a loop not by using its fields name and a dot operator manually.
My intention is to use this UserRegistration's object as an array so that I can iterate through its fields using a loop not by using its fields name and a dot operator manually.
Why? Or, to put it another way, why provide the getXXX() methods if you don't intend to use them? (*)
Associated with this is the observation that these fields model characteristics of the registration which are manifestly not strings. The dob? for instance are numeric. (but, better, dob is a date). Of course it you treat the fields as what they are (string/int/date/boolean etc) you might have to let go of the "array" business. The positive effect of this is that a Date instance has useful behaviour not shared by a triple of ints (much less of String instances).
If you really want such an array, provide another method in your class which creates an array of the appropriate length, populates it with the right values, and returns it.
[edit]
"so that I can iterate through its fields using a loop": I've just read more carefully. Still, why the for loop? Java!=JavaScript, as they say, and you would be looping over things of radically different types.
1) It sounds to me like you don't know about toString, and you're trying to duplicate its behavior in a really bad, awkward way.
Please consider implementing a toString method instead of whatever it is you're trying to do.
2) you should make the fields private.
3) you should possibly also get rid of all those getters. What is this object supposed to do?
My intention is to use this UserRegistration's object as an array so that I can iterate through its fields using a loop not by using its fields name and a dot operator manually.
Why? Or, to put it another way, why provide the getXXX() methods if you don't intend to use them? (*)
Amen.
Associated with this is the observation that these fields model characteristics of the registration which are manifestly not strings. The dob? for instance are numeric. (but, better, dob is a date). Of course it you treat the fields as what they are (string/int/date/boolean etc) you might have to let go of the "array" business. The positive effect of this is that a Date instance has useful behaviour not shared by a triple of ints (much less of String instances).
In addition, I'd suggest that 'gender' and 'marital_status' should probably be enums, and 'occupation' might well warrant a whole new class. What if somebody decided to put the string "green" in your 'gender' field, for example?
@OP: Like paulcw, I think you may be confusing what you want with implementing a 'toString()' method. However, if you genuinely want to create some way of preserving the contents of your objects, you could also look at Serialization.
Agree with the others above. Turning this meaningful bean back into a lump of raw data - an array - is a step backwards in terms of object orientation. You seem to imply that calling methods is wasteful in some way. Can you qualify that?