What is the difference between getFields and getDeclaredFields in Java reflection

I’m a little confused about the difference between the getFields method and the getDeclaredFields method when using Java reflection.

I read that getDeclaredFields gives you access to all the fields of the class and that getFields only returns public fields. If this is the case, why wouldn’t you just always use getDeclaredFields?

Can someone please elaborate on this, and explain the difference between the two methods, and when/why you would want to use one over the other?

4 Answers
4

Leave a Comment