0

Can I access and get the value of object using java reflection

ther is method to get --getLong,getInt ,but I couldn't find getObject()

1
  • 1
    Could you explain what you're trying to do? Maybe there is a better way to do what you want. Commented Mar 9, 2011 at 9:48

2 Answers 2

3

Are you looking for simply Field.get(Object obj)?

Sign up to request clarification or add additional context in comments.

3 Comments

BTW: You can use get() on all fields types including primitives.
And in order to access private fields you should call field.setAccessible(true);
@Noam: Yup. I'm assuming the OP is happy with getting at the field etc, and it's just the get method they couldn't find.
0

Class aClass = MyObject.class
Field field = aClass.getField("someField");

The example above will return the Field instance corresponding to the field someField
as declared in the MyObject below:

public class MyObject{
public String someField = null;
}

If no field exists with the name given as parameter to the getField() method, a
NoSuchFieldException is thrown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.