File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Values of Object o? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Values of Object o?" Watch "Values of Object o?" New topic
Author

Values of Object o?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a Object o with 2 values stored in o.

How can I get the values of o without having getter/setter (as no specialized Object is available) ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

"I have a Object o with 2 values stored in o. "
What do you mean stored in o?
To hold some value, it has to be assigned to some field.
Why don't you post your code to make things clearer?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I agree, some code would help...

nimo frey wrote:I have a Object o with 2 values stored in o.

How can I get the values of o without having getter/setter (as no specialized Object is available) ?


This is a problem, why can you not have a Type for the Object that would give you access to the fields you need? You should try to design your app such that the Object could, at least, be cast to an interface that would give you access to the fields.

Otherwise, you could try to use Reflection. To see more read: The Sun Reflection Tutorial


Steve
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

I am very much curious about this part "as no specialized Object is available".
Way I look at things, this means this is some Class from some standard library, either Sun or third party.
Who in his correct frame of mind would design a class with no getters? (unless of course the fields are public). Either way this looks like bad design.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Okay I have solved it via using a special Type for that Object and its getter/setter.

(I was just wondering, if there is a possibility to extract the values from an unspecified Object (Imagine, not to know the type of a Object, so Reflection is also not appropriate as not knowing the type). But it seems not possible.)

Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

nimo frey wrote:Okay I have solved it via using a special Type for that Object and its getter/setter.


That is the best thing to do. It will make things a lot easier down the road.

nimo frey wrote:(I was just wondering, if there is a possibility to extract the values from an unspecified Object (Imagine, not to know the type of a Object, so Reflection is also not appropriate as not knowing the type). But it seems not possible.)

Thanks.


Reflection can work when you don't know anything about the Type of the Object. The Object o would have access to its Class, which you could then dig through for members and such. Not very easy to use, hard to maintain, but you can do it, especially if you have some idea of what you are looking for.

Note: I am not saying you should use reflection. I am just saying it is possible.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
nimo frey wrote:Okay I have solved it via using a special Type for that Object and its getter/setter.
That is what I would usually regard as correct
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Values of Object o?