ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Subverting Java Access Protection for Unit Testing
Subject:   reason for getDeclaredFields()
Date:   2009-02-19 09:29:16
From:   mgobeil
I disagree with "The reason for the existence of two methods (instead of a single method that returns all fields, including inherited ones) seems to be so that simple dynamic lookup of public fields can be achieved easily (using getField() and getFields()) and generally does the right thing. If a program wants to see the private fields, it will probably want to handle inherited fields specially (for example, an object-oriented debugger)."


The real reason is probably that since private fields are hidden from subclasses, they're free to define new, unrelated fields with the same name and different type. getField() and getFields() would have no way of handling the duplicate fields through the inheritance chain.