File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Values of Object o?

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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?
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47288
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic